在网格中排列多面图 - 如何完全删除多面标签以避免重叠
Arrange faceted plots in a grid - how to completely remove facet labels to avoid overlapping
我对以下内容有点困惑:我想用 cowplot::plot_grid
将多个 (gg) 图排列到一个网格中。下面是一个有两个 ggplots(g_bottom
和 g_top
)的例子,都是多面的。底部的小平面标签已删除,因为它们是多余的。然而,似乎保留了背景的轮廓左右,充当用白线切割顶部图(见下图)。
我该如何解决这个问题?
到目前为止我尝试了什么:
而不是 strip.background = element_blank()
我也在 theme
中尝试了 strip.background = element_rect(fill = NA, color = NA)
,但没有成功。
如果我设置 rect = element_blank()
,它会以某种方式起作用,但我失去了整个情节边界。我当时希望 rect = element_rect(fill = "transparent", colour = NA)
会这样做,但仍然没有成功。我也刚刚尝试了 colour = NULL
或 colour = "transparent"
也没有成功。
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))
我可以通过不分面和单独创建 4 个图来解决这个问题,但也许有一些 theme
参数的更直接的解决方案,我太盲目了,看不到任何进一步的...
最终,在制作 g_bottom
时在 theme
中使用 rect = element_blank()
,然后添加 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))
我对以下内容有点困惑:我想用 cowplot::plot_grid
将多个 (gg) 图排列到一个网格中。下面是一个有两个 ggplots(g_bottom
和 g_top
)的例子,都是多面的。底部的小平面标签已删除,因为它们是多余的。然而,似乎保留了背景的轮廓左右,充当用白线切割顶部图(见下图)。
我该如何解决这个问题?
到目前为止我尝试了什么:
而不是 strip.background = element_blank()
我也在 theme
中尝试了 strip.background = element_rect(fill = NA, color = NA)
,但没有成功。
如果我设置 rect = element_blank()
,它会以某种方式起作用,但我失去了整个情节边界。我当时希望 rect = element_rect(fill = "transparent", colour = NA)
会这样做,但仍然没有成功。我也刚刚尝试了 colour = NULL
或 colour = "transparent"
也没有成功。
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))
theme
参数的更直接的解决方案,我太盲目了,看不到任何进一步的...
最终,在制作 g_bottom
时在 theme
中使用 rect = element_blank()
,然后添加 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))