R 为多面板ggplot添加一个图例

R 为多面板ggplot添加一个图例,r,plot,ggplot2,R,Plot,Ggplot2,我正在尝试使用ggplot创建一个带有单个图例的多面板绘图。我已经能够通过使用如下代码首先创建六个单独的绘图来创建多面板绘图: p1 <- ggplot(data, aes(y = value, x = Time))+ geom_point(position="dodge")+geom_line(aes(group=group,linetype=group)) 我现在想做的是为所有六个情节添加一个单独的图例,但似乎不明白。我读过一些关于在打印边界之外添加图例的内容,但似乎无法将

我正在尝试使用ggplot创建一个带有单个图例的多面板绘图。我已经能够通过使用如下代码首先创建六个单独的绘图来创建多面板绘图:

p1 <- ggplot(data, aes(y = value, x = Time))+     
geom_point(position="dodge")+geom_line(aes(group=group,linetype=group))

我现在想做的是为所有六个情节添加一个单独的图例,但似乎不明白。我读过一些关于在打印边界之外添加图例的内容,但似乎无法将其与pushViewport方法一起使用。

以下内容将使用elegend.justization='left',legend.position=c0,0.75在左上角显示一个图例

library(grid)
数据示例

x<-c(1,1,2,2)
y<-c(107, 110, 110, 118)
data<-data.frame(x,y)
data$group<-as.factor(c(1,2,1,2))
Ggplot

p1 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.justification = 'left', legend.position=c(0,0.75))
p2 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p3 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p4 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p5 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p6 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")

pushViewport(viewport(layout = grid.layout(3, 2)))  
print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))     
print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(p3,vp = viewport(layout.pos.row = 2, layout.pos.col = 1))     
print(p4,vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(p5,vp = viewport(layout.pos.row = 3, layout.pos.col = 1))     
print(p6,vp = viewport(layout.pos.row = 3, layout.pos.col = 2))

以下是hadley使用grid.arrange的解释:你为什么不刻面?您可以添加到p1是的,在facetting@CMichael上打了一个很好的电话。我不是在这里刻面,因为我希望添加一些独特的额外情节元素,例如,一些情节的重要性明星,而不是其他。人们知道有没有一种方法可以为一些但不是所有的情节添加一些附加的注释内容?
p1 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.justification = 'left', legend.position=c(0,0.75))
p2 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p3 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p4 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p5 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")
p6 <- ggplot(data, aes(y = y, x = x)) + geom_point(position="dodge") + geom_line(aes(group=group,linetype=group)) + theme(legend.position = "none")

pushViewport(viewport(layout = grid.layout(3, 2)))  
print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))     
print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(p3,vp = viewport(layout.pos.row = 2, layout.pos.col = 1))     
print(p4,vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
print(p5,vp = viewport(layout.pos.row = 3, layout.pos.col = 1))     
print(p6,vp = viewport(layout.pos.row = 3, layout.pos.col = 2))