我对以下内容有些困惑:我想用cowplot::plot_grid
将多个(gg)图布置到一个网格中。下面是一个具有两个ggplots(g_bottom
和g_top
)的示例,它们均是多面的。底部的构面标签已删除,因为它们是多余的。但是,似乎仍然保留了背景的轮廓,在顶部绘图中用白线进行了切割(请参见下图)。
我该如何解决?
到目前为止我尝试过什么:
我也在strip.background = element_blank()
中尝试了strip.background = element_rect(fill = NA, color = NA)
,但没有成功。
如果我设置了theme
,它将以某种方式起作用,但是我会丢失整个绘图边框。然后,我希望rect = element_blank()
可以做到,但仍然没有成功。我也刚刚尝试过rect = element_rect(fill = "transparent", colour = NA)
或colour = NULL
,但没有成功。
colour = "transparent"
我可以通过不进行构面和单独创建4个图的方式来解决该问题,但是也许有一个更简单的解决方案,其中包含一些library(ggplot2)
library(cowplot)
g <- ggplot(mpg, aes(class)) +
geom_bar() +
facet_grid(. ~ year) +
theme_bw()
g_bottom <- g +
theme(
strip.text = element_blank(),
strip.background = element_blank(),
# strip.background = element_rect(fill = NA, color = NA) # didn't work either
# Was hoping that this will do the trick, bot no success:
rect = element_rect(fill = "transparent", color = NA)
)
g_top <- g +
labs(x = element_blank()) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
plot_grid(g_top, NULL, g_bottom,
# used NULL to be able to tweak spacing between plots with rel_heights
align = "hv",
nrow = 3,
rel_heights = c(1, -0.2, 1))
参数,我实在是太盲目了,无法进一步了解... >
答案 0 :(得分:1)
最终,在制作rect = element_blank()
时在theme
中使用g_bottom
,然后添加panel.border = element_rect(colour = "black")
似乎可以解决问题。我仍然不明白为什么最初的试验无法按预期进行。
library(ggplot2)
library(cowplot)
g <- ggplot(mpg, aes(class)) +
geom_bar() +
facet_grid(. ~ year) +
theme_bw()
g_bottom <- g +
theme(
strip.text = element_blank(),
rect = element_blank(),
panel.border = element_rect(colour = "black")
)
g_top <- g +
labs(x = element_blank()) +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
plot_grid(g_top, NULL, g_bottom + theme(panel.border = element_rect(colour = "black")),
align = "hv",
nrow = 3,
rel_heights = c(1, -0.2, 1))