简体   繁体   中英

is there a way to adjust theme of a cowplot object?

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()

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

在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM