【问题标题】:Grid.arrange with zero margins / Mirrored barplots without spacing具有零边距的 Grid.arrange / 无间距的镜像条形图
【发布时间】: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 &lt;- PlotL + PlotR中合并

感谢任何提示!

【问题讨论】:

  • 如果您展示了您目前拥有的代码和足够的数据来重现您的问题,这将有所帮助。但另请参阅stackoverflow.com/a/36804394/2055765,这可能是解决您问题的更简单方法。
  • @Richard 谢谢你的评论。我已经添加了代码。你的链接我有帮助,不幸的是我无法用我的数据实现这个。我很感激任何帮助!

标签: r ggplot2 gridextra


【解决方案1】:

可以这样实现。由于您希望删除几乎所有主题元素,您可以简单地使用theme_void(),而不是删除添加所需的主题元素,如 y 轴。此外,我通过guide(fill="none") 删除了图例,并将绘图边距设置为零。最后,我为右图添加了轴线,并为左图反转了expansion

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))

library(ggplot2)

PR <- ggplot(DWP1, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
  theme_void() +
  geom_col(aes(fill = City), width = 0.1, position =  position_dodge(-0.9), linetype="dotted")+
  coord_flip() +
  scale_y_continuous(expand = expansion(mult = c(0, .1))) +
  theme(axis.line.y = element_line(), plot.margin = unit(rep(0, 4), "pt")) +
  guides(fill = "none")

PL <- ggplot(DWP2, aes(x = reorder(City, People), y = People,reorder(City,-People)))+
  theme_void() +
  geom_col(aes(fill = City), width = 0.1)+
  coord_flip()+
  scale_y_reverse(expand = expansion(mult = c(.1, 0))) +
  theme(plot.margin = unit(rep(0, 4), "pt")) +
  guides(fill = "none")

library(patchwork)

PL + PR

library(gridExtra)

grid.arrange(PL, PR, nrow = 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多