R' ggplot2'使用多个(网格化)绘图对象排列常见和独特的图例。

时间:2017-10-19 14:26:20

标签: r plot ggplot2 legend

编辑 - 可重现的示例包含在下面

我一直将this function与ggplot2包一起用于Add a common Legend for combined ggplots,当每个情节只有一种类型的图例时,这种情况非常有用。为...,color =, ...

然而,我正在尝试安排多个情节,这些情节共享一个共同的传说但每个都有另外一个独特的传说,例如:

  ggplot(df1, aes(x=Site, y=RESULT, color=Position , shape=DETNAME)) +
        geom_point(size=5) + ylab ("concentration (mg/L)") +
        labs (shape = "Determinand")

产生:

enter image description here

我有3x这些位置共享位置图例,但是Determinand图例是唯一的。

所以我想知道是否有一个额外的参数我可以传递给grid_arrange_shared_legend(),它将保留确定性和图例(shape = DETNAME),即使用类似{{{{{}}的方式将它们绘制在网格上的每个图上方。 1}}但是有一个共同的位置传奇(legend.position = "top")?

我知道我可以为每个绘图对象添加color = position,然后使用+ guides(shape = FALSE )给我共享的位置图例,但我希望实现类似这样的东西,但每个绘图都有一个唯一的确定性图例: enter image description here

或者任何人都可以建议grid_arrange_shared_legend()函数的源代码的哪一部分需要编辑才能执行此操作?

编辑 - 可重现的示例

grid_arrange_shared_legend()

enter image description here

在此处使用 library (ggplot2) library(gridExtra) library (grid) # two ggplot plot objects with multiple legends 1 common legend and 2 unique p1<- ggplot(diamonds, aes(x=price, y= depth, color= clarity , shape= cut )) + geom_point(size=5) + labs (shape = "unique legend", color = "common legend") p2 <- ggplot(diamonds, aes(x=price, y= depth, color= clarity , shape= color )) + geom_point(size=5) + labs (shape = "unique legend", color = "common legend") # shared legend function grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) { plots <- list(...) position <- match.arg(position) g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]] lheight <- sum(legend$height) lwidth <- sum(legend$width) gl <- lapply(plots, function(x) x + theme(legend.position="none")) gl <- c(gl, ncol = ncol, nrow = nrow) combined <- switch(position, "bottom" = arrangeGrob(do.call(arrangeGrob, gl), legend, ncol = 1, heights = unit.c(unit(1, "npc") - lheight, lheight)), "right" = arrangeGrob(do.call(arrangeGrob, gl), legend, ncol = 2, widths = unit.c(unit(1, "npc") - lwidth, lwidth))) grid.newpage() grid.draw(combined) # return gtable invisibly invisible(combined) } grid_arrange_shared_legend (p1,p2) 功能意味着唯一图例仅对网格上的其中一个图表是正确的

问题如何保存(提取?)独特的图例并将它们绘制在网格上的每个图上方,但是将常见图例保留在底部?

1 个答案:

答案 0 :(得分:4)

我建议在这里使用cowplot。在这种情况下,最简单的方法是将两个plot_grid调用合并,然后使用get_legend获取图例:

library(ggplot2)

#reduce the number of points to plot
diamonds2 <- diamonds[sample(nrow(diamonds), 500), ]

p1<- ggplot(diamonds2, aes(x=price, y= depth, color= clarity , shape= cut )) +
  geom_point(size=5) + labs (shape = "unique legend", color = "common legend") +
  theme(legend.position = "top")

p2 <- ggplot(diamonds2, aes(x=price, y= depth, color= clarity , shape= color )) +
  geom_point(size=5) + labs (shape = "unique legend", color = "common legend") +
  theme(legend.position = "top")

cowplot::plot_grid(
  cowplot::plot_grid(
    p1 + scale_color_discrete(guide = FALSE),
    p2 + scale_color_discrete(guide = FALSE),
    align = 'h'
  ),
  cowplot::get_legend(p1 + scale_shape(guide = FALSE) + theme(legend.position = "bottom")),
  nrow = 2, rel_heights = c(4, 1)
)

enter image description here