使用R覆盖两个/多个图形

时间:2016-09-13 04:33:52

标签: r ggplot2

我一直在引用stackoverflow帖子,并提出了对gglot2功能的查询。

用于代码的数据集如下。

TimeStamp   data_1      data_2      data_3      
15:11:37.474    99.791028   0.312498    99.47853    
15:16:22.373    99.791028   0.729162    99.061866   
15:21:37.424    99.791028   0.104166    99.686862   
15:31:52.475    88.02027            90.520254   
15:42:07.157    99.99936    0.208332    99.791028   
15:43:22.279    99.99936    0.52083     99.47853    
15:45:37.673    99.686862   0       99.686862   
15:52:52.872    99.686862   0.729162    98.9577 

p1<- ggplot(df, aes(TimeStamp, data_1,group=1)) + geom_point() + geom_point(data = df[df$data_1 > 80,], pch = 21, fill = NA, size = 4, colour = "red", stroke = 1) + geom_line()
p2<-ggplot(df, aes(TimeStamp, data_3,group=1)) + geom_point() + geom_point(data = df[df$data_3 > 70,], pch = 21, fill = NA, size = 4, colour = "blue", stroke = 1) + geom_line()

我正在尝试使用print(p1+p2).将P1和P2叠加到相同的X和Y轴上 但是,提出了以下错误。

Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+" 

这些图表的作用是什么;它将根据条件圈选某些数据点,并在同一区域显示两个图形。

1 个答案:

答案 0 :(得分:1)

试试这个

p1<- ggplot(df, aes(TimeStamp, data_1,group=1)) + geom_point(pch = 21, fill = NA, size = 4, colour = "red", stroke = 1)+geom_point()+geom_line(colour = "red")
p2 <- p1+geom_line(data=df, aes(TimeStamp, data_3),colour="blue")+
  geom_point(data=df, aes(TimeStamp, data_3,group=data_3),colour="blue",pch = 21, size = 4, stroke = 1,fill= NA)+geom_point(data=df, aes(TimeStamp, data_3,group=data_3))


print(p2)

enter image description here