R'ggplot2'将常见和唯一的图例与多个(网格化的)绘图对象一起排列. [英] R 'ggplot2' Arranging common and unique legends with multiple (gridded) plot objects.
问题描述
编辑-以下包含可复制的示例
我一直在使用此功能和ggplot2软件包一起添加到为组合的ggplots添加通用图例当每个情节只有一种类型的图例时,它可以完美地工作,例如对于...,color =, ...
.
I have been using this function with the ggplot2 package to Add a common Legend for combined ggplots which works perfecty when each plot has only 1 type of legend e.g. for ...,color =, ...
.
但是,我试图排列多个具有相同图例的图,但每个图都有一个额外的独特图例,例如:
However, I am trying to arrange multiple plots which share a common legend but each has an additional unique legend e.g.:
ggplot(df1, aes(x=Site, y=RESULT, color=Position , shape=DETNAME)) +
geom_point(size=5) + ylab ("concentration (mg/L)") +
labs (shape = "Determinand")
产生:
我有3倍的位置图例共享,但Determinand图例是唯一的.
I have 3x these where the Position legend is shared but the Determinand legends are unique.
所以我想知道是否还有一个我可以传递给grid_arrange_shared_legend()
的参数,该参数将保留Determinand图例(shape = DETNAME
),即使用legend.position = "top"
之类的图形将它们绘制在网格的每个图上方,位置(color = position
)的常见图例?
So I want to know if there is an additional argument I can pass to grid_arrange_shared_legend()
which will preserve the Determinand legends (shape = DETNAME
) i.e. plotting them above each plot on the grid using something like legend.position = "top"
but having a common legend for Position (color = position
) ?
我知道我可以将+ guides(shape = FALSE )
添加到每个绘图对象,然后使用grid_arrange_shared_legend()
,这给了我共享的位置图例,但是我想实现类似的功能,但是每个绘图都具有唯一的Determinand图例:
I know I can add + guides(shape = FALSE )
to each plot object and then use grid_arrange_shared_legend()
which gives me the shared Position legend, but I want to achieve something like this but with a unique Determinand legend for each plot:
或者有人可以建议grid_arrange_shared_legend()
函数的源代码的哪一部分需要编辑才能执行?
Or could any one advise which part of the source code for the grid_arrange_shared_legend()
function needs editing to perform this?
编辑-可复制的示例
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)
在这里使用grid_arrange_shared_legend()
函数意味着唯一的图例仅对网格上的其中一个图是正确的
Using the grid_arrange_shared_legend()
function here means that the unique legend is correct only for one of the plots on the grid
问题:如何保存(提取?)独特的图例并将其绘制在网格上的每个图上方,但将公共图例保留在底部?
Question How to preserve (extract?) the unique legends and plot them above each plot on the grid but keep the common legend at the bottom?
推荐答案
我建议在这里使用cowplot
.在这种情况下,最简单的方法是合并两个plot_grid
调用,然后使用get_legend
获取图例:
I would advise using cowplot
here. In this case it's easiest to combine two plot_grid
calls, then get the legend with 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)
)
这篇关于R'ggplot2'将常见和唯一的图例与多个(网格化的)绘图对象一起排列.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!