2

I'm trying to get my facet_grid() labels to wrap (using ggplot). I found the label_wrap_gen() function to use inside the labeller and actually got it working, and I don't remember changing anything about the code, but now it no longer works (???). I was able to reproduce the issue using the NC shapefile that comes with ggplot2.

This runs without throwing an error, but the labels aren't wrapped:

nc <- st_read(system.file("shape/nc.shp", package="sf"))

nc$grp <- c(rep('long group name 1', 33),
            rep('long group name 2', 33),
            rep('long group name 3', 34))

nc$cat <- rep(c('super duper long category name 1', 
            'super duper long category name 2', 
            'super duper long category name 3', 
            'super duper long category name 4'), 25)

nc <- nc %>% 
  mutate(grp = factor(grp, levels = unique(grp)),
         cat = factor(cat, levels = unique(cat)))

nc %>%
  ggplot() +
  geom_sf(aes(fill = AREA)) +
  coord_sf() +
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1),
        panel.background = element_blank(),
        panel.border = element_rect(color = "grey80", fill = NA),
        legend.title = element_text(),
        legend.position = "bottom") +
  facet_grid(grp ~ cat)

Changing the facet_grid() call to the below throws an error:

  ...
  facet_grid(grp ~ cat,
             labeller = labeller(cat = label_wrap_gen(8),
                                 grp = label_wrap_gen(10)))
# returns
Error in labeller(cat = label_wrap_gen(8), grp = label_wrap_gen(10)) : 
  unused arguments (cat = label_wrap_gen(8), grp = label_wrap_gen(10))

I've looked at this post about wrapping facet_grid() labels and this post about unused arguments in labeller(), but I haven't yet found anything that addresses what I'm seeing specifically.

What am I missing?

Laura
  • 119
  • 1
  • 9
  • This works fine for me on R v4.1.2 and ` ggplot2_3.3.5` - might be worth updating versions and trying again? – Andy Baxter Mar 17 '22 at 12:48
  • Strange. My `tidyverse` and `ggplot2` were up-to-date, and I'm running R 4.0.5. I restarted RStudio, and now it runs. Any idea what I might have run before the restart that mucked it up? – Laura Mar 17 '22 at 14:29
  • Very strange! not sure what could have caused it - I'd wonder if it was a different `labeller` function masking the `ggplot2::labeller` one (and using different arguments), but I don't know any other packages that use it. – Andy Baxter Mar 18 '22 at 10:23

1 Answers1

0

It seems like your package is maybe outdated. The code runs perfectly fine for me so I would suggest to run the following code:

remove.packages("tidyverse")
install.packages("tidyverse", dependencies = TRUE)
library(tidyverse)

After that you can just run your code:

   library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))

nc$grp <- c(rep('long group name 1', 33),
            rep('long group name 2', 33),
            rep('long group name 3', 34))

nc$cat <- rep(c('super duper long category name 1', 
                'super duper long category name 2', 
                'super duper long category name 3', 
                'super duper long category name 4'), 25)

nc <- nc %>% 
  mutate(grp = factor(grp, levels = unique(grp)),
         cat = factor(cat, levels = unique(cat)))

nc %>%
  ggplot() +
  geom_sf(aes(fill = AREA)) +
  coord_sf() +
  theme(axis.text.x = element_text(angle = 90, 
                                   hjust = 1),
        panel.background = element_blank(),
        panel.border = element_rect(color = "grey80", fill = NA),
        legend.title = element_text(),
        legend.position = "bottom") +
  facet_grid(grp ~ cat,
             labeller = labeller(cat = label_wrap_gen(8),
                                 grp = label_wrap_gen(10)))

Output: enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53