1

I have a plot using facet_grid() and would like to modify the facet grid's entries.

Consider the following MWE:

library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_grid(rows = vars(drv))

I would like to replace the entries 4, f, and r on the right hand side of each row with characters (e.g. c("Case 1", "Case 2", "Case 3") and add a title box right of the entries (i.e. between the grey box and the legend (cyl).

The documentation for facet_grid hasn't helped me - can someone point me to the right direction?

Ivo
  • 3,890
  • 5
  • 22
  • 53

2 Answers2

2

You would have to provide a labelling function to the labeller argument in facet_grid(), which uses a named character vector as lookup table.

library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_grid(rows = vars(drv),
             labeller = as_labeller(c("4" = "Case1", "f" = "Case2", "r" = "Case3")))

Created on 2020-05-28 by the reprex package (v0.3.0)

EDIT:

To use an extra strip layer as a spanning title, you could use facet_nested() from ggh4x (full disclaimer: I build that package).

library(ggplot2)
library(ggh4x)

ggplot(mpg, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_nested(rows = vars("title", drv),
             labeller = as_labeller(c("4" = "Case1", "f" = "Case2", 
                                      "r" = "Case3", "title" = "My Title Here")))

Created on 2020-05-28 by the reprex package (v0.3.0)

If you don't particularly care about the strip being there, you could use a secondary y-axis guide.

library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_grid(rows = vars(drv),
             labeller = as_labeller(c("4" = "Case1", "f" = "Case2", "r" = "Case3"))) +
  guides(y.sec = guide_none("My Title Here"))

Created on 2020-05-28 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Awesome, thank you. Is there any way to also add a "title" box for these three cases between the grey boxes and the `cyl` legend? – Ivo May 28 '20 at 12:45
  • Like an extra overarching strip? – teunbrand May 28 '20 at 12:46
  • Exactly, like a grey strip overarching all cases (dynamically; I have multiple "Cases" in my real plot)... – Ivo May 28 '20 at 12:48
  • @teunbrand Please consider putting the "follow-up question regarding the "overarching" in a seperate question-answer. This has little to do with the grid title naming. In my opinion, your addendum deserves a proper entry, and it will be easier to search for people looking for it. – KoenV May 28 '20 at 12:56
  • @KoenV I think people typically find it through this answer https://stackoverflow.com/a/55911134/11374827 – teunbrand May 28 '20 at 13:00
  • Understood. Thx – KoenV May 28 '20 at 13:00
  • The package looks amazing - thank you for that! It also offers a lot of opportunities I didn't even think of! :) – Ivo May 28 '20 at 13:14
  • Is the error `Error: (converted from warning) package 'ggplot2' was built under R version 3.6.3` (using R 3.6.3) a known one? I keep experiencing it and am unable to install the package. – Ivo May 28 '20 at 13:15
  • I downloaded R3.6.3 locally and got this error: `Error: Failed to install 'ggh4x' from GitHub: (converted from warning) cannot remove prior installation of package ‘ellipsis’`. It went away once I reinstalled the 'ellipsis' package – teunbrand May 28 '20 at 13:23
1

You could also consider adding a mutated column to your data, like in the following:

library(dplyr)
mpg_2 <- mpg %>%
  mutate(drv_2 = case_when(
    drv == "4" ~ "Case1",
    drv == "f" ~ "Case2",
    drv == "r" ~ "Case3" 
  ))

ggplot(mpg_2, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_grid(rows = vars(drv_2))
KoenV
  • 4,113
  • 2
  • 23
  • 38