How can I change the facet axis labels in ggplot2? 2

How can I change the facet axis labels in ggplot2?

The process of changing the facet axis labels in ggplot2 involves modifying the labels of the x and y axes within each facet of a plot. This can be achieved by accessing the facet labels through the “labs” function and using the “x” and “y” arguments to specify the desired labels. By customizing the facet axis labels, one can effectively enhance the visual representation and clarity of their ggplot2 plots.

Change Facet Axis Labels in ggplot2


You can use the as_labeller() function to change facet axis labels in ggplot2:

ggplot(df, aes(x, y)) + 
  geom_point() +
  facet_wrap(.~group,
             strip.position = 'left', 
             labeller = as_labeller(c(A='new1', B='new2', C='new3', D='new4'))) +
  ylab(NULL) +
  theme(strip.background = element_blank(),
        strip.placement='outside')

This particular example replaces the following old labels:

  • A, B, C, D

with the following new labels:

  • new1, new2, new3, new4

The following example shows how to use this syntax in practice.

Example: Change Facet Axis Labels in ggplot2

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'D', 'D'),
                 points=c(8, 14, 20, 22, 25, 29, 30, 31),
                 assists=c(10, 5, 5, 3, 8, 6, 9, 12))

#view data frame
df

  team points assists
1    A      8      10
2    A     14       5
3    B     20       5
4    B     22       3
5    C     25       8
6    C     29       6
7    D     30       9
8    D     31      12

The following code shows how to use facet_wrap() to create a grid that displays a scatterplot of assists vs. points for each team:

library(ggplot2)

#create multiple scatter plots using facet_wrap
ggplot(df, aes(assists, points)) +
  geom_point() +
  facet_wrap(.~team, nrow=4)

Currently the facets have the following labels: A, B, C, D.

However, we can use the following code to change the labels to team A, team B, team C, and team D:

library(ggplot2)

#create multiple scatter plots using facet_wrap with custom facet labels
ggplot(df, aes(assists, points)) + 
  geom_point() +
  facet_wrap(.~team, nrow=4,
             strip.position = 'left', 
             labeller = as_labeller(c(A='team A',
                                      B='team B',
                                      C='team C',
                                      D='team D'))) +
  ylab(NULL) +
  theme(strip.background = element_blank(),
        strip.placement='outside')

ggplot2 change facet axis labels

Note: The strip.background argument removes the grey background behind the facet labels and the strip.placement argument specifies that the labels should be placed outside of the axis ticks.

The following tutorials explain how to perform other common tasks in ggplot2:

Cite this article

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

stats writer. "How can I change the facet axis labels in ggplot2?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-facet-axis-labels-in-ggplot2/.

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

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

[1] stats writer, "How can I change the facet axis labels in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

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

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