有没有办法调整 cowplot 对象的主题?
is there a way to adjust theme of a cowplot object?
我可以更改组合剧情的主题吗?
示例:
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)
这个有效:
p1 + theme_bw()
这不起作用:
p + theme_bw()
简短的回答是“否”。
您在 cowplot 中看到的地块已被渲染为 grobs。他们没有您可以更改的主题元素。理论上,您可以“触及”埋藏在 p 结构中的 grob,并一次更改一个 grob,但这是困难、混乱且不可靠的。
作为展示它有多么棘手的练习,下面是如何将 p
更改为 theme_bw
外观:
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
我可以更改组合剧情的主题吗?
示例:
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)
这个有效:
p1 + theme_bw()
这不起作用:
p + theme_bw()
简短的回答是“否”。
您在 cowplot 中看到的地块已被渲染为 grobs。他们没有您可以更改的主题元素。理论上,您可以“触及”埋藏在 p 结构中的 grob,并一次更改一个 grob,但这是困难、混乱且不可靠的。
作为展示它有多么棘手的练习,下面是如何将 p
更改为 theme_bw
外观:
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