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

Leave a Reply

Your email address will not be published.