R'ggplot2'用多个(网格化)绘图对象排列常见和独特的图例。
编辑 - 包括以下R'ggplot2'用多个(网格化)绘图对象排列常见和独特的图例。
我已经使用this function与GGPLOT2包再现的例子Add a common Legend for combined ggplots其中工程perfecty当每个小区仅具有1型传说例如为...,color =, ...
。
不过,我想安排其共用一个传说多条曲线,但每个人都有一个额外的独特传奇如:
ggplot(df1, aes(x=Site, y=RESULT, color=Position , shape=DETNAME)) +
geom_point(size=5) + ylab ("concentration (mg/L)") +
labs (shape = "Determinand")
主要生产:
我有3倍以下位置图例是共享的,但决定和图例是唯一的。
所以我想知道是否有一个额外的参数,我可以传递给grid_arrange_shared_legend()
将保留Determinand传说(shape = DETNAME
),即绘制它们使用类似legend.position = "top"
网格中的每个情节之上,但具有位置共同的图例( color = position
)?
我知道我可以添加+ guides(shape = FALSE)
到每个情节对象,然后使用grid_arrange_shared_legend()
使我有共同立场的传说,但我想实现这样的事情,但有独特Determinand传说每个情节:
或者任何人都可以建议grid_arrange_shared_legend()
函数的源代码的哪一部分需要编辑来执行此操作?
编辑 - 可重复的例子
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()
功能是指独特的传说只能对电网
问题的曲线图的一个是正确的如何保留(提取?)独特的图例并将它们绘制在网格上的每个图上方,但保留常见的图例底部?
我会建议在这里使用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)
)