0

I have three dataset, they are three distinguished bird's assemblages and I'm trying to merge the test results in one plot together. Each one looks like this:

Exemple:

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)

the result that I need is like this:

plot_merged

I could do this:

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

but still not enough, because couldn't plot lines...

Spyzaetus
  • 3
  • 5
  • Each `stat_summary()` call can take `data=` parameter. Just keep adding to the same `ggplot` object but with different data sources. Or better yet, combine the data with an indicator variable before plotting. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. (pictures of data aren't reproducible) – MrFlick Nov 06 '19 at 03:10
  • Hi MrFlick, thank you for your advice. I tried adding layer by layer with stat summary but lines connect doesn't work. but I could merge two test result (as mean-point, only). I'll keep searching for. – Spyzaetus Nov 06 '19 at 04:24

2 Answers2

1

So I think this will approximately give what you want. We just combine the beta1 and beta2 in 1 data.frame and plot that:

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

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Hi teunbrand. Thank you for your help! I could do this earlier but I fell in sleep. My solution was quite similar. I used excel and put one column named "region" and then input my data again and did a bit different than your. But your solve is quite more "clean". And I could solve other question by myself. – Spyzaetus Nov 06 '19 at 11:11
0

In addition, I solve other question of mine: "If I want to do the same but with boxplot, how can I do this?"

I tried this: 1.Step - by teunbrand (answer above).

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

Thank you guys!

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Spyzaetus
  • 3
  • 5