Dan*_* C. 10 whitespace r legend ggplot2 patchwork
我用这个ggplot2包制作了 3 个地块。为了将绘图排列在一个图中,我使用了该patchwork包。在安排中,我将 2 个图放在顶部,在这些图下方放置通用图例,在通用图例下方放置第三个图。我使用该函数创建了公共图例空间guide_area(),但随之创建了一个大的未使用的空白区域。
如何才能将未使用的空白空间保持在最低限度?
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = mpg,
aes(x = fl,
y = displ)) +
geom_col(aes(fill = cty))
p2 <- ggplot(data = mpg,
aes(x = year,
y = hwy)) +
geom_point(aes(color = drv))
p3 <- ggplot(data = mpg,
aes(x = class,
y = displ)) +
geom_col() +
facet_grid(~year)
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect") &
theme(legend.position = "bottom")
Run Code Online (Sandbox Code Playgroud)
空白区域保留在图形的不同大小和比例中(空白区域用红色标记)。
heights = ...里面用plot_layout。
例如,
((p1+p2)/guide_area()/p3) +
plot_layout(guides = "collect", heights = c(3,1,3)) &
theme(legend.position = "bottom")
Run Code Online (Sandbox Code Playgroud)