The Complete Guide: How to Change Font Size in ggplot2


You can use the following syntax to change the font size of various elements in ggplot2:

p + theme(text=element_text(size=20), #change font size of all text
        axis.text=element_text(size=20), #change font size of axis text
        axis.title=element_text(size=20), #change font size of axis titles
        plot.title=element_text(size=20), #change font size of plot title
        legend.text=element_text(size=20), #change font size of legend text
        legend.title=element_text(size=20)) #change font size of legend title   

The following examples show how to use this syntax with the following scatterplot in ggplot2:

library(ggplot2)

#create data frame
df <- data.frame(x=c(1, 2, 3, 4, 5, 6),
                 y=c(6, 8, 14, 19, 22, 18),
                 z=c('A', 'A', 'B', 'B', 'C', 'C'))

#create scatterplot
p <- ggplot(df, aes(x=x, y=y, color=z)) +
       geom_point(size=3) +
         ggtitle("This is the Title")

p

Example 1: Change Font Size of All Text

The following code shows how to change the font size of all text elements in the plot:

p + theme(text=element_text(size=20))

ggplot change font size

Example 2: Change Font Size of Axis Text

The following code shows how to change the font size of just the axis text:

p + theme(axis.text=element_text(size=30))

ggplot2 change font size of axis text

Example 3: Change Font Size of Axis Titles

The following code shows how to change the font size of just the axis titles:

p + theme(axis.title=element_text(size=30))

ggplot2 change font size of axis labels

Example 4: Change Font Size of Plot Title

The following code shows how to change the font size of just the plot title:

p + theme(plot.title=element_text(size=30))

ggplot change font size of plot title

Example 5: Change Font Size of Legend Text

The following code shows how to change the font size of just the legend text:

p + theme(legend.text=element_text(size=30))

ggplot2 change size of legend text

Example 6: Change Font Size of Legend Title

The following code shows how to change the font size of just the legend title:

p + theme(legend.title=element_text(size=30))

ggplot2 change font size of legend title

Additional Resources

The Complete Guide to ggplot2 Titles
How to Change Legend Size in ggplot2
How to Rotate Axis Labels in ggplot2

5 Replies to “The Complete Guide: How to Change Font Size in ggplot2”

  1. Thanks for this useful quick reference guide. I’m a bit puzzled when trying to use theme(text=element_text()) to set text size globally for a graph. Your example 1 shows that this doesn’t change the font size of all the graph elements to 20pt: for example, the axis titles are larger than the axis labels, and I think the plot title is larger still. Do you know exactly what theme(text = element_text()) is doing? Does this define the size for some elements of the graph (perhaps the axis labels), and then scales the other elements around that?

  2. Isn’t there an easy, quick way to increase/decrease all text elements by a factor??

    let’s say I want to double the size of both title, and axis labels in a single command? pretty standard…

  3. Thank all Gods and Deities and bless your immortal soul for making this so clear and usable. Too many others make a complete mess of teaching the most basic things in R. Technology is often made inaccessible via poor writing and unnecessarily complicating things.

  4. Hi,
    Thank you for your kind sharing. I have a code like this, but the font size of the percentages shown on the bar is too big. is it possible to change it? Thank you.
    > ggplot(LanPin, aes(x = ECMO_to_infection_duration, y = Frequency, fill = NI_pathogens))+
    + geom_col()+
    + scale_y_continuous(breaks = seq(0,75, by = 5))+
    + geom_text(aes(label = paste(format(round(Percentage, 2),nsmall = 2),”%”)), position = position_stack(0.5))

Leave a Reply

Your email address will not be published. Required fields are marked *