Nasser
Nasser

Reputation: 2140

Plotting Error bar in R

I have the following dataset:

            conf    variable        value
1           C1      T1      0.6578166
2           C2      T1      0.6729989
3           C3      T1      0.6629129
4           C4      T1      0.6602419
5           C5      T1      0.6951054
6           C1      T2      0.6764889
7           C2      T2      0.7008580
8           C3      T2      0.6921985
9           C4      T2      0.6868215
10          C5      T2      0.7104877
11          C1      T3      0.6834098
12          C2      T3      0.7120974
13          C3      T3      0.7045760
14          C4      T3      0.6986261
15          C5      T3      0.7180474
16          C1      T4      0.6875082
17          C2      T4      0.7185950
18          C3      T4      0.7122448
19          C4      T4      0.7051376
20          C5      T4      0.7229975
21          C1      T5      0.6902167
22          C2      T5      0.7228775
23          C3      T5      0.7167748
24          C4      T5      0.7093825
25          C5      T5      0.7261777
....

I want to plot these data as a line plot with error bars. To do that, I used the summarySE function that is defined in this website. I want the plot to be a line plot where the x axis represents the variable and the y axis represents the value and group by conf. The error bar should be presented with each variable considering the values from T1 to the point of the current variable. For example, the considered values for the error bar in T6 should be the values from T1 to T6.

Based on that, I called the summarySE function as:

m2 <- summarySE(m, measurevar = "value", groupvars = c("variable","conf") , na.rm=TRUE)

The results that I got are:

     variable    conf N  value     sd se ci
1    T1          C1   1  0.6578166 NA NA NA
2    T1          C2   1  0.6729989 NA NA NA
3    T1          C3   1  0.6951054 NA NA NA
4    T1          C4   1  0.6629129 NA NA NA
5    T1          C5   1  0.6602419 NA NA NA
6    T2          C1   1  0.6764889 NA NA NA
......

I also tried the following:

m2 <- summarySE(m, measurevar = "value", groupvars = c("conf") , na.rm=TRUE)

I got the following results:

             conf  N     value          sd          se          ci
1              C1 60 0.7038542 0.009609437 0.001240573 0.002482381
2              C2 60 0.7395447 0.012869318 0.001661422 0.003324497
3              C3 60 0.7418830 0.010397609 0.001342326 0.002685987
4              C4 60 0.7365407 0.014438260 0.001863971 0.003729798
5              C5 60 0.7301954 0.014389284 0.001857649 0.003717146

But that does not work to plot the error bar with every variable.

To draw the plot:

coverage_plot <- ggplot(m2, aes(x=m$variable, y=m$value, group=conf, color=conf)) +
      geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=0.1, position=pd) +
      geom_line(position=pd) + geom_point(position=pd) +
      scale_x_discrete(labels = c(1:60))

When I run the code, I also get the following error:

Error: Aesthetics must be either length 1 or the same as the data (5): ymin, ymax, x, y, group, colour

Can someone help me solving this problem?

Upvotes: 0

Views: 176

Answers (2)

Denis Cousineau
Denis Cousineau

Reputation: 497

You could go with a package design to built mean plots such as superb (summary plots with error bars). Here are some variations:

library(superb)
# basic plot; bars are used by default
superb(value ~ conf, dta)

#line plot
superb(value ~ conf, dta, plotStyle = "line")

#line plot with some additional directive for customizing it
library(ggplot2)
superb(value ~ conf, dta, plotStyle = "line") +
  theme_bw() + ylab("Variable")

Edit

As said by @Nate in a comment, you only have one observations per cell in your post. Hence, for the moment, we can only do a plot by variable or by conf. If you have the complete set of observations, you can use

superb(value ~ conf + variable, dta)

Note that I am the creator of superb.

Upvotes: 0

Koundy
Koundy

Reputation: 5523

Try removing $ in aes like this

coverage_plot <- ggplot(m2, aes(x=variable, y=value, group=conf, color=conf)) +
      geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=0.1, position=pd) +
      geom_line(position=pd) + geom_point(position=pd) +
      scale_x_discrete(labels = c(1:60))

Also there is no column called variable in your data. Make sure you are writing correct name

Upvotes: -1

Related Questions