How to Change GGPlot Facet Labels



How to Change GGPlot Facet Labels

 

This article describes how to change easily ggplot facet labels.

Contents:

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Prerequisites

Load required packages and set the theme function theme_light() [ggplot2] as the default theme:

library(ggplot2)
theme_set(
  theme_light() + theme(legend.position = "top")
  )

Basic ggplot with facet

Create a box plot filled by groups:

# Load data and convert dose to a factor variable
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Box plot, facet accordding to the variable dose and supp
p <- ggplot(ToothGrowth, aes(x = dose, y = len)) + 
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9)) +
  scale_fill_viridis_d() 
p + facet_grid(dose ~ supp)

Change the text of facet labels

Facet labels can be modified using the option labeller, which should be a function.

  • In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.
p + facet_grid(dose ~ supp, labeller = label_both)

  • A simple way to modify facet label text, is to provide new labels as a named character vector:
# New facet label names for dose variable
dose.labs <- c("D0.5", "D1", "D2")
names(dose.labs) <- c("0.5", "1", "2")

# New facet label names for supp variable
supp.labs <- c("Orange Juice", "Vitamin C")
names(supp.labs) <- c("OJ", "VC")

# Create the plot
p + facet_grid(
  dose ~ supp, 
  labeller = labeller(dose = dose.labs, supp = supp.labs)
  )

  • An alternative solution to change the facet labels, is to modify the data:
df <- ToothGrowth
# Modify the data
df$dose <- factor(df$dose, levels = c("0.5", "1", "2"), 
                  labels = c("D0.5", "D1", "D2"))
df$supp <- factor(df$supp, levels = c("OJ", "VC"),
                  labels = c("Orange Juice", "Vitamin C")
                  )
# Create the plot
ggplot(df, aes(x = dose, y = len)) + 
  geom_boxplot(aes(fill = supp)) +
  facet_grid(dose ~ supp)

Customize facet labels appearance

# Change facet text font. Possible values for the font style:
  #'plain', 'italic', 'bold', 'bold.italic'.
p + facet_grid(dose ~ supp)+
    theme(
      strip.text.x = element_text(
        size = 12, color = "red", face = "bold.italic"
        ),
      strip.text.y = element_text(
        size = 12, color = "red", face = "bold.italic"
        )
      )

Change facet background color

The rectangle around facet labels can be modified using the function element_rect().

p + facet_grid(dose ~ supp)+
 theme(
   strip.background = element_rect(
     color="black", fill="#FC4E07", size=1.5, linetype="solid"
     )
   )





Comments ( 4 )

  • Another great and very useful post,
    (with clear code examples).

    Thanks you again, Kassambara.
    You are a Pro!.

    SFer
    San Francisco

    • Thank you SFer for your feedback, always appreciated!

  • Fringse

    Thanks for these information!
    On top of what you describe here: How can I change the font of only once facet and leave the others untouched?
    Say have one facet label in italic and the others in plain?

    • Hi,

      I don’t know how to modify the font of only one panel. If you find any solution, I would appreciate if you can share it.

Post a Reply