如何将来自不同数据集的绘图图层合并到一个绘图中-ggplot2?

时间:2019-11-06 02:52:26

标签: r ggplot2

我有三个数据集,它们是三个杰出的鸟类组合,我正尝试将测试结果合并到一个图中。每个人看起来像这样:

示例:

# netstat -plant | grep httpd
tcp        0      0 :::80                   :::*                    LISTEN      98/httpd
tcp        0      0 :::443                  :::*                    LISTEN      98/httpd

我需要的结果是这样的:

plot_merged

我可以这样做:

library(ggplot2)
beta1<-c(0.714286,0.625,0.72973,0.5625,0.733333,1,0.655172,0.92,0.769231,0.586207,0.724138,0.846154,
0.833333,0.76,1)
group<-rep(c("q0", "q1", "q2"), each = 5)

beta2<-c(1.714286,1.625,1.72973,1.5625,1.733333,1,1.655172,1.92,1.769231,1.586217,1.724138,1.846154,
1.833333,1.76,1)


dados1<-data.frame(beta1, group)
dados2<-data.frame(beta2, group)

p1<-ggplot(data=dados1, aes(x=group, y=beta1)) +
  stat_summary(fun.y=mean, geom="line", aes(group=1))  + 
  stat_summary(fun.y=mean, geom="point")+ylim(0,2)

p2<-ggplot(data=dados2, aes(x=group, y=beta2)) +
  stat_summary(fun.y=mean, geom="line", aes(group=1))  + 
  stat_summary(fun.y=mean, geom="point")+ylim(0,2)

但仍然不够,因为无法绘制线条...

2 个答案:

答案 0 :(得分:1)

因此,我认为这大致可以满足您的需求。我们只将beta1beta2合并为1 data.frame并作图:

dados1 <-data.frame(beta = beta1, group, id = "beta1")
dados2 <-data.frame(beta = beta2, group, id = "beta2")

df <- rbind(dados1, dados2)

ggplot(df, aes(group, beta, colour = id, group = id)) +
  stat_summary(fun.y=mean, geom="line")  + 
  stat_summary(fun.y=mean, geom="point") + 
  ylim(0,2)

enter image description here

答案 1 :(得分:0)

此外,我还解决了我的另一个问题:“如果要使用箱线图进行同样的操作,我该怎么做?”

我尝试了这个: 1.Step-通过teunbrand(上面的答案)。

dados1 <-data.frame(beta = beta1, group, id = "beta1") 
dados2 <-data.frame(beta = beta2, group, id = "beta2")

 df <- rbind(dados1, dados2)

2.step:

ggplot(df, aes(group, beta, colour = id, facet_grid= id))+ geom_boxplot()

Plot

谢谢你们!