Textual Healing

November 14, 2012
By

(This article was first published on is.R(), and kindly contributed to R-bloggers)

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 his blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.