This article describes how to change ggplot axis labels (or axis title). This can be done easily using the R function labs()
or the functions xlab()
and ylab()
.
In this R graphics tutorial, you will learn how to:
- Remove the x and y axis labels to create a graph with no axis labels. For example to hide x axis labels, use this R code:
p + theme(axis.title.x = element_blank())
. - Change the font style of axis labels (size, color and face).
Contents:
Key ggplot2 R functions
- Start by creating a box plot using the
ToothGrowth
data set:
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
geom_boxplot()
- Change x and y axis labels as follow:
- p + xlab(“New X axis label”): Change the X axis label
- p + ylab(“New Y axis label”): Change the Y axis label
- p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels
- Key ggplot2 theme options to change the font style of axis titles:
theme(
axis.title = element_text(), # Change both x and y axis titles
axis.title.x = element_text(), # Change x axis title only
axis.title.x.top = element_text(), # For x axis label on top axis
axis.title.y = element_text(), # Change y axis title only
axis.title.y.right = element_text(), # For y axis label on right axis
)
Arguments of the function element_text()
includes:
- color, size, face, family: to change the text font color, size, face (“plain”, “italic”, “bold”, “bold.italic”) and family.
- lineheight: change space between two lines of text elements. Number between 0 and 1. Useful for multi-line axis titles.
- hjust and vjust: number in [0, 1], for horizontal and vertical adjustment of axis titles, respectively.
hjust = 0.5
: Center axis titles.hjust = 1
: Place axis titles on the righthjust = 0
: Place axis titles on the left
- To remove a particular axis title, use
element_blank()
instead ofelement_text()
, for the corresponding theme argument.
For example to remove all axis titles, use this: p + theme(axis.title = element_blank())
.
Change axis labels
# Default plot
print(p)
# Change axis labels
p <- p + labs(x = "Dose (mg)", y = "Teeth length")
p
Change label size, color and face
- Key functions:
theme()
andelement_text()
- Allowed values for axis titles font face: “plain”, “italic”, “bold” and “bold.italic”
p + theme(
axis.title.x = element_text(color = "blue", size = 14, face = "bold"),
axis.title.y = element_text(color = "#993333", size = 14, face = "bold")
)
Remove axis labels
Key function: use element_blank()
to suppress axis labels.
p + theme(axis.title.x = element_blank(),
axis.title.y = element_blank())
Remove all axis titles at once:
p + theme(axis.title = element_blank())
Conclusion
Change a ggplot x and y axis titles as follow:
p + labs(x = " x labels", y = "y labels")+
theme(
axis.title.x = element_text(size = 14, face = "bold"),
axis.title.y = element_text(size = 14, face = "bold.italic")
)
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Stanford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Version: Français
No Comments