The appliance of geom_line() in ggplot()

2019-08-03 08:31发布

问题:

I have used the ggplot() to show the mean and variance of my data but I do not know why although I have used geom_line() the result is not connected as a line according to the experiment. The data frame is just an example sorry if it is weird.

dfc
  m1   s1 sd1.5      sd1.10 sd1.15 sd1.20 sd1.25 sd1.30
1 10  n=4     1 experiment1     15     20     25     30
2 12  n=8     1 experiment1     15     20     25     30
3 14 n=12     2 experiment1     15     20     25     30
4 13  n=4     1 experiment2     15     20     25     30
5 16  n=8     2 experiment2     15     20     25     30
6 19 n=12     1 experiment2     15     20     25     30

ggplot(dfc, aes(x=s1,y=m1,colour=sd1.10)) + 
  geom_errorbar(aes(ymin=m1-sd1.5,ymax=m1+sd1.5),width=0.1)+
  geom_line()+
  geom_point()

many thanks

回答1:

Just add aes(group = sd1.10) to geom_line to tell ggplot which points belong to one group:

ggplot(dfc, aes(x=s1,y=m1,colour=sd1.10)) + 
  geom_errorbar(aes(ymin=m1-sd1.5,ymax=m1+sd1.5),width=0.1)+
  geom_line(aes(group = sd1.10))+
  geom_point()



标签: r ggplot2