Add Standard Error Bars to Barchart (2 Examples) | Draw Barplot in Base R & ggplot2 | stat_summary()
HTML-код
- Опубликовано: 3 июл 2022
- How to create a barplot with standard error (SE) bars in the R programming language. More details: statisticsglobe.com/add-stand...
R code of this video:
set.seed(37924) # Create example data frame
data <- data.frame(values = rnorm(100, 2),
group = letters[1:4])
data_summary <- aggregate(values ~ group, data, # Create summary data
function(x) c(mean = mean(x),
se = sd(x) / sqrt(length(x))))
data_summary <- data.frame(group = data_summary[ , 1], data_summary$values)
data_summary # Print summary data
base_r_barplot <- barplot(data_summary$mean ~ group, # Draw and store Base R barplot
data_summary,
ylim = c(0, 2.7))
arrows(x0 = base_r_barplot, # Add error bars
y0 = data_summary$mean + data_summary$se,
y1 = data_summary$mean - data_summary$se,
angle = 90,
code = 3,
length = 0.1)
install.packages("ggplot2") # Install & load ggplot2 package
library("ggplot2")
ggplot(data, aes(values, group, fill = group)) + # ggplot2 barplot with error bars
coord_flip() +
stat_summary(geom = "bar", fun = mean, position = "dodge") +
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge")
Follow me on Social Media:
Facebook - Statistics Globe Page: / statisticsglobecom
Facebook - R Programming Group for Discussions & Questions: / statisticsglobe
Facebook - Python Programming Group for Discussions & Questions: / statisticsglobepython
LinkedIn - Statistics Globe Page: / statisticsglobe
LinkedIn - R Programming Group for Discussions & Questions: / 12555223
LinkedIn - Python Programming Group for Discussions & Questions: / 12673534
Twitter: / joachimschork
Music by bensound.com
HI, thank you for the video!
Could you please explain what line 9 does?
I would expect it to change data_summary to a data frame type but typeof() says its still a list
( I'm very new to R)
Hello Galia,
Thank you for pointing that out. It's an understandable confusion. Actually, it is a matter of programming terminology of R. If you use class() instead of typeof(), you will see the output is a data frame. However, it is hard to explain why for me. But you can check the discussion on Cross Validated, I think it would give you an idea. See stats.stackexchange.com/questions/3212/mode-class-and-type-of-r-objects
Regards,
Cansu
Dear Statistics Globe: I always wonder on your important videos, that is why I am following your RUclips channel every occasion:
On July 05/2022 you uploaded a video about “Add Standard Error Bars to Barchart (2 Examples) _ Draw Barplot in Base R & ggplot2” I have tried my best to import the r script to RStudio and run all the script. However, the last function that is presented below is imported properly according to your procedure.
library(ggplot2)
ggplot(data, aes(values, group, fill = group)) + # ggplot2 barplot with error bars
coord_flip() +
stat_summary(geom = "bar", fun = mean, position = "dodge") +
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge")
but on the console the result says
Warning message:
Computation failed in `stat_summary()`:
Can't convert `fun`, a double vector, to a function.
So, what is wrong with my analysis with RStudio version 4.2.0? I hope I will expect good response from you soon, thank you.
Hey Zeru, thanks a lot for the kind comment, it's great to hear that you like my videos! Have you used the same data as I did in my example? I've executed the code once again and everything worked fine. Regards, Joachim
Why all the error bars are of same length
Hello Nitish,
I think they are different but very close to each other. This is because the group values are samples from the same normal distribution. If we let them come from different distributions, you can see an apparent difference among the error plots. See the example below.
# Set the seed for reproducibility
set.seed(37924)
# Create values for each group
values_a