I need some help to figure out to estimate the standard error using the following R script:
library(ggplot2)
library(ggpubr)
library(Hmisc)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth, 4)
theme_set(
theme_classic()
theme(legend.position = "top")
)
# Initiate a ggplot
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
# Add mean points /- SD
# Use geom = "pointrange" or geom = "crossbar"
e geom_violin(trim = FALSE)
stat_summary(
fun.data = "mean_sdl", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
# Combine with box plot to add median and quartiles
# Change fill color by groups, remove legend
e geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")
Many thanks for the help Kind regards
CodePudding user response:
A couple of things. First, you need to reassign e
when you add geom_violin
and stat_summary
. Otherwise, it isn't carrying those changes forward when you add the boxplot in the next step. Second, when you add the boxplot last, it is mapping over the points and error bars from stat_summary
so it looks like they're disappearing. If you add the boxplot first and then stat_summary
the points and error bars will be placed on top of the boxplot. Here is an example:
library(ggplot2)
library(ggpubr)
library(Hmisc)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
theme_set(
theme_classic()
theme(legend.position = "top")
)
# Initiate a ggplot
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
# Add violin plot
e <- e geom_violin(trim = FALSE)
# Combine with box plot to add median and quartiles
# Change fill color by groups, remove legend
e <- e geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")
# Add mean points /- SE
# Use geom = "pointrange" or geom = "crossbar"
e
stat_summary(
fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
You said in a comment that you couldn't see any changes when you tried mean_se
and mean_cl_normal
. Perhaps the above solution will have solved the problem, but you should see a difference. Here is an example just comparing mean_se
and mean_sdl
. You should notice the error bars are smaller with mean_se
.
ggplot(ToothGrowth, aes(x = dose, y = len))
stat_summary(
fun.data = "mean_sdl", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
ggplot(ToothGrowth, aes(x = dose, y = len))
stat_summary(
fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black"
)
Here is a simplified solution if you don't want to reassign at each step:
ggplot(ToothGrowth, aes(x = dose, y = len))
geom_violin(aes(fill = dose), trim = FALSE)
geom_boxplot(width = 0.2)
stat_summary(fun.data = "mean_se", fun.args = list(mult = 1),
geom = "pointrange", color = "black")
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
theme(legend.position = "none")