Textual Healing

[This article was first published on is.R(), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

While I know there are several awesome guides for how to make fine-tuning adjustments to plot labels/axes/ticks/text in ggplot, this is still the most common question I get from people new to ggplot: how do I change the size/font/color/position of (some text element)? Here are some examples. 

First, let’s load the made-up data from yesterday.

Our default plot looks like this:

#Adjust X Axis Label Size/Face/Color:

Plot.1 + theme(axis.title.x = element_text(face=”bold”, 

                                           colour=”dodgerblue”, 

                                           size=14) )

To Adjust Y Axis Label Size/Face/Color:

Plot.1 + theme(axis.title.y = element_text(face=”italic”, 

                                           colour=”darkred”, 

                                           size=24) )

When adjusting multiple parameters, you can use specify both within the “theme” command:

Plot.1 + theme(axis.title.x = element_text(face=”bold”, 

                                           colour=”dodgerblue”, 

                                           size=14),

               axis.title.y = element_text(face=”italic”, 

                                           colour=”darkred”, 

                                           size=24) )

Changing Main Title Text/Face/Color  

Plot.1 + theme(plot.title = element_text(family=”sans”,

                                           face=”bold”, 

                                           colour=”darkblue”, 

                                           size=44) )

When modifying the text in the legend, you can adjust either the legend title text…

Plot.1  + theme(legend.title = element_text(colour=”dodgerblue”, 

                                            size=26, 

                                            face=”bold”))

…or the text for the legend labels:

Plot.1 + theme(legend.text = element_text(colour=”goldenrod4”, 

                                          size = 18, 

                                          face = “italic”))

Or by doing both simultaneously:

Plot.1 + theme(legend.title = element_text(colour=”dodgerblue”, 

                                           size=26, 

                                           face=”bold”),

               legend.text = element_text(colour=”goldenrod4”, 

                                          size = 18, 

                                          face = “italic”))

Another option in the element_text() is angle, which is useful in certain occasions, especially when you have tight axis tick labels:

These can be adjusted using the angle command for the axis.text.x element:

Plot.1  + theme(axis.text.x = element_text(colour=”black”, 

                                           size = 11, 

                                           face = “bold.italic”,

                                           angle=45,

                                           vjust=1,

                                           hjust=1))

Tomorrow’s Post will show how to adjust the facet strips…stay tuned! Full code/script appears below:

To leave a comment for the author, please follow the link and comment on their blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)