R 镶嵌面网格中的不同绘图类型

R 镶嵌面网格中的不同绘图类型,r,ggplot2,R,Ggplot2,我再一次面对一个复杂的情节。我想使用镶嵌面栅格在一个绘图中绘制不同的绘图类型 我希望我能用以下例子说明我的观点: 我想生成一个类似于第一张图片的绘图,但上面的绘图应该类似于第二张图片。 我已经找到了使用子集函数的技巧,但我不能只在一个绘图中添加垂直线,更不用说两到三条或指定颜色 代码: 据我所知,不能使用facet.grid在一个绘图中放置多个绘图类型。在我看来,你的两个选择是 将空数据放入第一个方面,使行“存在”但不显示,或 使用视口将多个打印合并为一个打印的步骤 我认为第二种解决方案更一般,

我再一次面对一个复杂的情节。我想使用镶嵌面栅格在一个绘图中绘制不同的绘图类型

我希望我能用以下例子说明我的观点: 我想生成一个类似于第一张图片的绘图,但上面的绘图应该类似于第二张图片。 我已经找到了使用子集函数的技巧,但我不能只在一个绘图中添加垂直线,更不用说两到三条或指定颜色

代码:


据我所知,不能使用facet.grid在一个绘图中放置多个绘图类型。在我看来,你的两个选择是

将空数据放入第一个方面,使行“存在”但不显示,或

使用视口将多个打印合并为一个打印的步骤

我认为第二种解决方案更一般,所以我就是这么做的:

#name each of your plots
p2 <- ggplot(subset(dfr_melt, variable=="a"),aes(x=d,y=value)) + facet_grid(variable~.,scales="free")+
  geom_line(subset=.(variable=="a")) + geom_line(subset=.(variable=="b"))

#Upper plot should look like this
p1 <- ggplot(dfr,aes(x=d,y=a)) + geom_line() + geom_line(aes(y=c,color="c"))+
  geom_hline(aes(yintercept=1),linetype="dashed")+
  geom_hline(aes(yintercept=-2),linetype="dashed")

#From Wickham ggplot2, p154
vplayout <- function(x,y) {
  viewport(layout.pos.row=x, layout.pos.col=y)
}

require(grid)
png("myplot.png", width = 600, height = 300) #or use a different device, e.g. quartz for onscreen display on a mac
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 1)))
print(p1, vp=vplayout(1, 1))
print(p2, vp=vplayout(2, 1))
dev.off()

你可能需要稍微摆弄一下,让他们排好队。关闭上部绘图上的镶嵌面,并将下部绘图上的图例移到底部,应该可以做到这一点。

据我所知,不能使用facet.grid在一个绘图中放置多个绘图类型。在我看来,你的两个选择是

将空数据放入第一个方面,使行“存在”但不显示,或

使用视口将多个打印合并为一个打印的步骤

我认为第二种解决方案更一般,所以我就是这么做的:

#name each of your plots
p2 <- ggplot(subset(dfr_melt, variable=="a"),aes(x=d,y=value)) + facet_grid(variable~.,scales="free")+
  geom_line(subset=.(variable=="a")) + geom_line(subset=.(variable=="b"))

#Upper plot should look like this
p1 <- ggplot(dfr,aes(x=d,y=a)) + geom_line() + geom_line(aes(y=c,color="c"))+
  geom_hline(aes(yintercept=1),linetype="dashed")+
  geom_hline(aes(yintercept=-2),linetype="dashed")

#From Wickham ggplot2, p154
vplayout <- function(x,y) {
  viewport(layout.pos.row=x, layout.pos.col=y)
}

require(grid)
png("myplot.png", width = 600, height = 300) #or use a different device, e.g. quartz for onscreen display on a mac
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 1)))
print(p1, vp=vplayout(1, 1))
print(p2, vp=vplayout(2, 1))
dev.off()

你可能需要稍微摆弄一下,让他们排好队。关闭上部图上的刻面,并将下部图上的图例移到底部,应该可以做到这一点。

如果我正确理解您的问题,您只需在dfr中添加一个变量列,即可使刻面工作:

dfr$variable = "a"
ggplot(subset(dfr_melt, variable=="a"),aes(x=d,y=value)) +  
  facet_grid(variable~.,scales="free")+
  geom_line(data=subset(dfr_melt,variable=="a"))  + 
  geom_line(data=subset(dfr_melt, variable=="b")) + 
  geom_line(data=dfr, aes(y=c, colour=factor(c))) + 
  geom_hline(aes(yintercept=1),linetype="dashed")+
  geom_hline(aes(yintercept=-2),linetype="dashed")
请注意,我的绘图没有Z字形,这是因为我更改了:

  #This is almost certainly not what you want
  geom_line(data=dfr, aes(y=c, colour="c"))


如果我正确理解了您的问题,您只需要在dfr中添加一个变量列,以允许镶嵌面工作:

dfr$variable = "a"
ggplot(subset(dfr_melt, variable=="a"),aes(x=d,y=value)) +  
  facet_grid(variable~.,scales="free")+
  geom_line(data=subset(dfr_melt,variable=="a"))  + 
  geom_line(data=subset(dfr_melt, variable=="b")) + 
  geom_line(data=dfr, aes(y=c, colour=factor(c))) + 
  geom_hline(aes(yintercept=1),linetype="dashed")+
  geom_hline(aes(yintercept=-2),linetype="dashed")
请注意,我的绘图没有Z字形,这是因为我更改了:

  #This is almost certainly not what you want
  geom_line(data=dfr, aes(y=c, colour="c"))


顺便说一句:谁能告诉我为什么我的问候总是被省略。看起来我是一个不友好的人,我不是!不鼓励添加问候语:请在代码中添加一些空格,以便阅读。@Andrie周围没有空格,非常感谢你不知道,顺便问一下:有人能告诉我为什么我的问候总是被省略吗。看起来我是一个不友好的人,我不是!不鼓励添加问候语:请在代码中添加一些空格,以便阅读。@Andrie周围缺少空格非常感谢您不知道您使用的是哪个ggplot2版本?对于0.9.2.1,我有一个错误,原因是。在子集notationAlso 0.9.2.1中,当我重新运行它时,我也会得到一个错误。不知道发生了什么-很明显我曾经策划过一次。现在没有时间进行故障排除。请使用哪个ggplot2版本?对于0.9.2.1,我有一个错误,原因是。在子集notationAlso 0.9.2.1中,当我重新运行它时,我也会得到一个错误。不知道发生了什么-很明显我曾经策划过一次。现在没有时间进行故障排除。