【发布时间】:2021-07-01 09:34:58
【问题描述】:
我正在尝试连接两个镜像的 ggplot 条形图。我想使用grid.arrange 来连接轴上的两个单独的图。不幸的是,我总是在情节之间留有空间。如何完全减少边距,以便将两个图连接在一个轴上?
这就是我到目前为止所尝试的:
DWP1 <- data.frame("City" = c("Berlin", "Paris", "London"),
"People" = c(3.3, 2.1, 9))
DWP2 <- data.frame("City" = c("New York", "Washington", "Miami"),
"People" = c(8.4, 0.7, 0.4))
PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1, position = position_dodge(-0.9), linetype="dotted")+
xlab("")+
coord_flip()+
theme(axis.title.y = element_text(angle = 0, size=0, vjust = 0.5, family= Schriftart, color="black"))+
scale_y_continuous(expand = expansion(mult = c(0, .1)))
PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
theme(axis.line = element_line(),
panel.background = element_rect(fill = "transparent", color = NA),
plot.background = element_rect(fill= "transparent", color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position = "none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.line.x=element_blank(),
axis.line.y=element_blank(),
)+
geom_col(aes(fill = City), width = 0.1)+
xlab("")+
coord_flip()+
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_y_reverse()
PL + PR
这是当前外观的图片。两个图应该完全合并。
最后两个地块应该在PlotLR: PlotLR <- PlotL + PlotR中合并
感谢任何提示!
【问题讨论】:
-
如果您展示了您目前拥有的代码和足够的数据来重现您的问题,这将有所帮助。但另请参阅stackoverflow.com/a/36804394/2055765,这可能是解决您问题的更简单方法。
-
@Richard 谢谢你的评论。我已经添加了代码。你的链接我有帮助,不幸的是我无法用我的数据实现这个。我很感激任何帮助!