13.2 C
London
Tuesday, July 2, 2024
HomeTidyverse in Rggplot2 in RThe Complete Guide: How to Change Font Size in ggplot2

The Complete Guide: How to Change Font Size in ggplot2

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories