'Getting error in labeller...unused arguments

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quinten