【问题标题】:How to calculate standard error instead of standard deviation in ggplot如何在ggplot中计算标准误差而不是标准差
【发布时间】:2022-01-21 05:03:13
【问题描述】:

我需要一些帮助来确定使用以下 R 脚本估计标准误差:

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

非常感谢您的帮助 亲切的问候

【问题讨论】:

  • 您可以改用fun.data = "mean_se"
  • 这能回答你的问题吗? Standard error bars using stat_summary
  • @teunbrand 我用 mean_se 表示标准误差,还用 mean_cl_normal 表示 95%CI 和标准差;但我看不到它们之间的任何变化。您能否检查一下我的原始问题的编辑版本,现在也有截图。感谢 cmets
  • 这是stat_summary() 版本的情节,我不知道如何用箱线图来做。请注意,当您使用stat_summary() 时,您不会保存对象。因此,当您制作箱线图时,它没有stat_summary()

标签: r ggplot2


【解决方案1】:

有几件事。首先,您需要在添加geom_violinstat_summary 时重新分配e。否则,当您在下一步中添加箱线图时,它不会继续进行这些更改。其次,当您最后添加箱线图时,它会映射来自stat_summary 的点和误差线,因此看起来它们正在消失。如果您先添加箱线图,然后stat_summary 点和误差线将放置在箱线图的顶部。这是一个例子:

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

您在评论中说您在尝试mean_semean_cl_normal 时看不到任何更改。也许上面的解决方案已经解决了这个问题,但你应该看到不同之处。这是一个比较mean_semean_sdl 的示例。您应该注意到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"
  )

如果您不想在每一步都重新分配,这是一个简化的解决方案:

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

【讨论】:

  • 非常感谢您的提示;它工作得很好:-) 你好样的
猜你喜欢
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 2014-02-27
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多