Change Font Size of ggplot2 Facet Grid Labels in R (Example)

 

In this R tutorial you’ll learn how to increase or decrease the text size of the labels of a ggplot2 facet grid.

Table of contents:

It’s time to dive into the examples!

 

Creating Example Data

We’ll use the following data frame as basement:

set.seed(121222)                                                     # Create example data
data <- data.frame(x = sample(1:3, 100, replace = TRUE),
                   y = runif(100),
                   group = sample(c("Group1", "Group2", "Group3"),
                                  100, replace = TRUE))
head(data)                                                           # Print first rows
#   x          y  group
# 1 1 0.09529733 Group2
# 2 3 0.01889447 Group2
# 3 3 0.95745660 Group1
# 4 1 0.21935601 Group2
# 5 3 0.74697434 Group3
# 6 3 0.79233791 Group1

As you can see based on the previously shown output of the RStudio console, our data consists of three columns (i.e. x, y, and group) and 100 rows.

If we want to draw a facet grid with the ggplot2 package, we need to install and load the package to R:

install.packages("ggplot2")                                          # Install ggplot2
library("ggplot2")                                                   # Load ggplot2

Now, we can create a facet grid showing our example data as follows:

ggp <- qplot(x, y, data = data) +                                    # Create facet grid
  facet_grid(. ~ group)
ggp                                                                  # Print facet grid

 

facet grid ggplot2 r

Figure 1: Default Font Size of Labels.

 

As you can see in Figure 1, the previous R code created a ggplot2 facet grid with default font size of the labels. In the following, I’ll explain how to increase these labels…

 

Example: Increasing Text Size of Facet Grid Labels

If we want to modify the font size of a ggplot2 facet grid, we can use a combination of the theme function and the strip.text.x argument. In the following R syntax, I’m increasing the text size to 30. The larger/smaller this number is, the larger/smaller is the font size of the labels.

ggp +                                                                # Change font size
  theme(strip.text.x = element_text(size = 30))

 

facet grid ggplot2 r font size labels

Figure 2: Increased Font Size of Labels.

 

Compare Figure 2 with Figure 1: The font size of the labels of Figure 2 is much larger!

 

Video, Further Resources & Summary

Do you want to know more about how to change font sizes and drawing ggplot2 plots in R? Then you could have a look at the following video of my YouTube channel. In the video, I’m showing the R syntax of this article.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you might read the other tutorials of my website. You can find some articles here.

 

You learned in this article how to change the size of label elements in the R programming language. Don’t hesitate to let me know in the comments section, if you have additional questions.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top