How can I change the font size in ggplot2? 2

How can I change the font size in ggplot2?

“GGplot2 is a popular data visualization package in R that allows users to create customizable and visually appealing graphs. One of the key features of ggplot2 is the ability to change the font size in the plots. This can be achieved by using the ‘theme’ function and specifying the desired font size for elements such as axis labels, titles, and legends. This functionality provides users with the flexibility to adjust the font size according to their preferences and enhance the overall appearance of their graphs.”

The Complete Guide: 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

Cite this article

stats writer (2024). How can I change the font size in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-change-the-font-size-in-ggplot2/

stats writer. "How can I change the font size in ggplot2?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-font-size-in-ggplot2/.

stats writer. "How can I change the font size in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-change-the-font-size-in-ggplot2/.

stats writer (2024) 'How can I change the font size in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-change-the-font-size-in-ggplot2/.

[1] stats writer, "How can I change the font size in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I change the font size in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top