Skip to content

Instantly share code, notes, and snippets.

@debruine
Created October 13, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debruine/6b9110468649aa7f9883f04437f3a6a3 to your computer and use it in GitHub Desktop.
Save debruine/6b9110468649aa7f9883f04437f3a6a3 to your computer and use it in GitHub Desktop.
cowplot's plot_grid function doesn't work with labels if a custom theme has been set using theme()
library(ggplot2)
## set custom theme (even with no options)
theme_set(theme())
## Error: Aesthetics must be either length 1 or the same as the data (1): colour and family
a <- ggplot(cars, aes(speed, dist)) + geom_point()
cowplot::plot_grid(a, a, labels = c("A", "B"))
## Works with no labels
cowplot::plot_grid(a, a)
## Works when theme reset
theme_set(theme_classic())
cowplot::plot_grid(a, a, labels = c("A", "B"))
@Gootjes
Copy link

Gootjes commented Oct 13, 2020

Because the custom theme does not set the required aesthetic for cowplot:::draw_plot_label to use.
This works:
theme_set(theme(text=element_text(colour = "black", font= "Times"))
And this works as well: cowplot::plot_grid(a, a, labels = c("A", "B"), label_fontfamily = "Times", label_colour = "black")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment