1

Can I change the theme of a combined plot?

Example:

library(ggplot2)
library(cowplot)
p1 = ggplot(diamonds, aes(x = price)) + geom_histogram(bins = 30)
p2 = ggplot(diamonds, aes(x = table)) + geom_histogram(bins = 30)
p = cowplot::plot_grid(p1,p2)

This works:

p1 + theme_bw()

This does not work:

p + theme_bw()
invictus
  • 1,821
  • 3
  • 25
  • 30

1 Answers1

1

The short answer is "no".

The plots you see in a cowplot have already been rendered as grobs. They don't have theme elements you can change. You could theoretically "reach in" to the grobs buried inside the structure of p and change individual grobs one at a time, but this is difficult, messy and unreliable.

As an exercise to show how tricky it is, here is how you would change p into a theme_bw appearance:

p

enter image description here

p$layers <- lapply(p$layers, function(x) {
  y <- x$geom_params[[1]][[1]][[6]][[4]][[1]][[4]]
  y[[1]][[9]]$fill <- "white"
  y[[1]][[9]]$col <- "black"
  y[[4]][[7]]$col <- "gray90"
  y[[5]][[7]]$col <- "gray90"
  y[[2]][[7]]$col <- "gray90"
  y[[3]][[7]]$col <- "gray90"
  x$geom_params[[1]][[1]][[6]][[4]][[1]][[4]] <- y
  x
})

p

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87