如何对齐并避免单独创建然后添加到图中的多个图例重叠?

Ele*_*ino 7 r ggplot2 cowplot

我正在尝试创建一个结合了 2 个独立图例和多个绘图网格的绘图。我遇到的问题是我发现很难对齐图例以使它们可见且不重叠。希望下面的例子能够解释我的意思。

首先,我将创建 2 个图。在这两个图中,我只对图例感兴趣,并且我放弃了实际的图(所以请忽略这两个图中的实际图)。为了获得图例,我正在使用该cowplot包。

library(ggplot2)
library(cowplot)
# -------------------------------------------------------------------------
# plot 1 ------------------------------------------------------------------

# create fake data
dfLegend_1 <- data.frame(x = LETTERS[1:10], y = c(1:10))
# set colours
pointColours <- c(A = "#F5736A", B = "#D58D00", C = "#A0A300",
                  D = "#36B300", E = "#00BC7B", F = "#00BCC2",
                  G = "#00ADF4", H = "#928DFF", I = "#E568F0",
                  J = "#808080")

# plot
ggLegend_1 <- ggplot(dfLegend_1, aes(x=x, y=y))+
  geom_point(aes(fill = pointColours), shape = 22, size = 10) +
  scale_fill_manual(values = unname(pointColours),
                    label = names(pointColours),
                    name = 'Variable') +
  theme(legend.key.size = unit(0.5, "cm")) +
  theme_void()

# get legend
legend_1 <- get_legend(ggLegend_1)


# -------------------------------------------------------------------------
# plot 2 ------------------------------------------------------------------


# Create fake data
dflegend_2 <- data.frame(
  x = runif(100),
  y = runif(100),
  z2 = abs(rnorm(100))
)

# plot
ggLegend_2 <- ggplot(dflegend_2, aes(x=x, y = y))+
  geom_point(aes(color = z2), shape = 22, size = 10) +
  scale_color_gradientn(
    colours = rev(colorRampPalette(c('steelblue', '#f7fcfd', 'orange'))(5)),
    limits = c(0,10),
    name = 'Gradient',
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ))

# get legend
legend_2 <- get_legend(ggLegend_2)

Run Code Online (Sandbox Code Playgroud)

然后我创建许多图(在本例中,我创建 20 个单独的图)并将它们绘制在网格上:

# create data
dfGrid <- data.frame(x = rnorm(10), y = rnorm(10))

# make a list of plots
plotList <- list()
for(i in 1:20){
  plotList[[i]] <- ggplot(dfGrid) +
    geom_ribbon(aes(x = x, ymin = min(y), ymax = 0), fill = "red", alpha = .5) +
    geom_ribbon(aes(x = x, ymin = min(0), ymax = max(y)), fill = "blue", alpha = .5) +
    theme_void()
}

# plot them on  a grid
gridFinal <- cowplot::plot_grid(plotlist = plotList)
Run Code Online (Sandbox Code Playgroud)

最后,我将这两个图例连接在一起,并将它们添加到许多图的网格中:

# add legends together into on single plot
legendFinal <- plot_grid(legend_2, legend_1, ncol = 1)

# plot everything on the same plot
plot_grid(gridFinal, legendFinal,  rel_widths = c(3, 1))
Run Code Online (Sandbox Code Playgroud)

这会产生如下所示的结果: 示例图

正如您所看到的,图例重叠并且间隔不是很好。我想知道是否有任何方法可以容纳所有内容,同时使图例适当间隔且可读?

我还应该注意到,一般来说,可以有任意数量的变量和任意数量的网格图。

ste*_*fan 5

解决问题的一种选择是切换到patchwork将图和图例粘合在一起。特别是我利用design参数为图例分配更多空间Variable。但是,您应该意识到,与绘图相比,图例的灵活性要低得多,即图例的大小采用绝对单位,并且不会根据可用空间进行调整。因此,我不确定我的解决方案是否符合您对“一刀切”方法的渴望。

library(patchwork)

design <- 
"
ABCDEU
FGHIJV
KLMNOV
PQRSTV
"

plotList2 <- c(plotList, list(legend_2, legend_1))

wrap_plots(plotList2) +
  plot_layout(design = design)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 是的,我目前正在使用一种围绕三列的解决方案(每个图例都在一个单独的列中)。它实际上并没有解决问题......如果手动调整绘图窗口大小,重叠仍然存在。但我会继续寻找解决方案。 (2认同)