In this article, you will learn how to modify ggplot labels, including main title, subtitle, axis labels, caption, legend titles and tag.
- Plot title and subtitle provides insights into the main findings
- Caption are generally used to describe the data source
- Tag can be used for differentiating between multiple plots.
Load required packages and set the theme function
theme_classic() as the default theme:
Create a basic plot using the dataset
# Convert the variable dose from numeric to factor variable ToothGrowth$dose <- as.factor(ToothGrowth$dose) # Create a boxplot colored by dose group levels bxp <- ggplot(ToothGrowth, aes(x = dose, y = len)) + geom_boxplot(aes(color = dose)) + scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07")) bxp
Key R functions
labs(..., title = waiver(), subtitle = waiver(), caption = waiver(), tag = waiver()) xlab(label) ylab(label) ggtitle(label, subtitle = waiver())
...: A list of new name-value pairs. The name should be an aesthetic. For example
p + labs(title = "Main title", x = "X axis label", y = "Y axis label")changes main title and axis labels.
title: plot main title.
subtitle: the text for the subtitle for the plot which will be displayed below the title.
caption: the text for the caption which will be displayed in the bottom-right of the plot by default.
tag: the text for the tag label which will be displayed at the top-left of the plot by default.
label: the title of the respective axis (for xlab() or ylab()) or of the plot (for ggtitle()).
Add titles and axis labels
In this section, we’ll use the function
labs() to change the main title, the subtitle, the axis labels and captions.
It’s also possible to use the functions
ylab() to modify the plot title, subtitle, x and y axis labels.
Add a title, subtitle, caption and change axis labels:
bxp <- bxp + labs(title = "Effect of Vitamin C on Tooth Growth", subtitle = "Plot of length by dose", caption = "Data source: ToothGrowth", x = "Dose (mg)", y = "Teeth length", tag = "A") bxp
Modify legend titles
You can use
labs() to changes the legend title for a given aesthetics (fill, color, size, shape, . . . ). For example:
p + labs(fill = "dose")for geom_boxplot(aes(fill = dose))
p + labs(color = "dose")for geom_boxplot(aes(color = dose))
- and so on for linetype, shape, etc
bxp + labs(color = "Dose (mg)")
Split long titles
If the title is too long, you can split it into multiple lines using \n. In this case you can adjust the space between text lines by specifying the argument
lineheight in the theme function
bxp + labs(title = "Effect of Vitamin C on Tooth Growth \n in Guinea Pigs")+ theme(plot.title = element_text(lineheight = 0.9))
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
- 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
- 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
Amazing Selling Machine
- Free Training - How to Build a 7-Figure Amazon FBA Business You Can Run 100% From Home and Build Your Dream Life! by ASM
Books - Data Science
- 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)
- 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