Add Standard Error Bars to Barchart (2 Examples) | Draw Barplot in Base R & ggplot2 | stat_summary()

preview_player
Показать описание
R code of this video:

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 # 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)

library("ggplot2")

ggplot(data, aes(values, group, fill = group)) + # ggplot2 barplot with error bars
coord_flip() +
stat_summary(geom = "bar", fun = mean, position = "dodge") +

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Dear Statistics Globe: I always wonder on your important videos, that is why I am following your YouTube 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.

zeruyimer
Автор

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)

SaintGooch
Автор

Why all the error bars are of same length

Geography_