简体   繁体   中英

ggplot: error bars

I want to plot error bars of the two different set of value of y1, y2 with respect to x. In other words, I have two data Y1,Y2 and they are correspond X value. I managed to plot them together after I reshaped the data frame. Now I want to graph the error bars on the same graph for each Y1, Y2 points. I understand geom_errorbar() is what I'm looking for. However, I'm following long way to do that and I'm sure there is a short way. What I'm doing I'm calculating "se" for each set and calculate aes(ymin=y1-se, ymax=y+se) and repeat the same for Y2. Because I want to apply this error bars to different plots . I 'd rather do it in a short way.

Here my data frame after reshape:

M     Req      Rec      load       Un      L1
1    30.11  9.000000   3.000000  30.02000  A
2    50.31  10.030000  6.045000  39.44000  A
3    60.01  11.290000  7.366667  54.93000  A
4    66.10  12.630000  8.827500  68.44500  A
5    80.18  13.106000  9.462000  71.07600  A
6    87.10  14.421667  15.961667 82.70500  A
7    90.08  15.880000  20.644286 94.20714  A
1    4.000  1.500000    1.000000  1        B
2    8.240  6.240000    4.760000  3.00000  B
3    10.28  12.230000  9.420000  4.05000   B
4    18.570 25.570000 17.930000  6.00000   B
5    22.250 35.250000 27.850000  7.00000   B
6    35.070 55.010000 36.810000  8.06000   B
7    48.480  0.420000 47.020000  9.06000   B

I have used the following command to graph it:

ggplot(df_reshaped,aes(x = M, y = Req, colour = L1, shape=L1)) +  
     geom_point(size = 5)+
     geom_line() +
     scale_x_discrete(name="M") +
     scale_y_continuous(name="Y1 Y2")+
     ggtitle("A vs B")

In this case I'm graphing Y1=Req1, Y2=Req2, with respect to x=M

Any short way or suggestion to calculate the error bars ?

Is there any quick way to calculate the "se" ?

In general there are two possibilities to prepare your data for ggplot :

  • You could aggregate the raw data and plot the results. If you follow this way, you have to calculate the standard errors too since the information cannot be retrieved from the aggregated data. These standard errors could be plotted with geom_errorbar .
  • A second option is to use the raw data and let ggplot do all the calculations for you. This could be done with stat_summary . For example:

     stat_summary(fun.data = "mean_cl_normal", mult = 1, geom = "errorbar") 

Obviously, you have chosen the first approach. So, you just need to calculate the standard errors for the points of both variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM