1

我正在使用patchwork以顶部为中心的公共图例创建组合图。

library(ggplot2)
library(patchwork)

x1 <- ggplot(iris,aes(Sepal.Width, Petal.Length, col=Species))+
       geom_point()

x2 <- ggplot(iris,aes(Petal.Width, Petal.Length, col=Species))+
       stat_ellipse(show.legend=F)

wrap_plots(x1, x2, guides="collect") +
  theme(legend.position="top",
        legend.direction="horizontal")

这就是我得到的。

在此处输入图像描述

这是我所期待的。

ggpubr::ggarrange(x1, x2, common.legend=T)

在此处输入图像描述

4

1 回答 1

1

问题是您使用+的是&. 有关运算符之间的差异,请参见此处:

  1. +主题层仅适用于最后一个情节,即在x2您的情况下没有图例
  2. 如果你想申请所有地块,你必须利用&
library(ggplot2)
library(patchwork)

x1 <- ggplot(iris,aes(Sepal.Width, Petal.Length, col=Species))+
  geom_point()

x2 <- ggplot(iris,aes(Petal.Width, Petal.Length, col=Species))+
  stat_ellipse(show.legend=F)

wrap_plots(x1, x2, guides="collect") &
  theme(legend.position="top",
        legend.direction="horizontal")

于 2021-01-12T13:16:28.850 回答