Errorbar in ggplot is using SD or SE as default

399 Views Asked by At

I use stat_boxplot(geom="errorbar", width=.3) to add an errorbar to my plot. But I do not know the errorbar means standard deviation (SD) or standard error (SE). I wanna make sure my description is right. Do any guys know this and please give a help.

Thanks a lot.

2

There are 2 best solutions below

0
On BEST ANSWER

Check out the instructions:

First, it is necessary to summarize the data. This can be done in a number of ways, as described on this page. In this case, we’ll use the summarySE() function defined on that page, and also at the bottom of this page. (The code for the summarySE function must be entered before it is called here).

1
On

Clearly, AK88's answer is correct and captures the key point. However, I always found the reference to the r-cookbook and its summarySE() approach a little indirect and potentially confusing. I would offer the following approach based on pipes exemplified using mtcars:

require(tidyverse)
mtcars %>%
summarise(mpgSD = sd(mpg),
        mpg = mean(mpg),
        lower = mpg - mpgSD,
        upper = mpg + mpgSD) %>%
ggplot(aes(x=1, y=mpg)) + geom_point() + geom_linerange(aes(ymin=lower, ymax=upper)) + ylim(0,30)

Using dplyr's summarise you can easily define your lower and upper values and those are referenced in geom_errorbar / geom_linerange ...