r - 如何在 R 中按组计算 SD,而不丢失在 ggplot2 中绘图仍需要的列?

标签 r ggplot2 dplyr summarize

我有一个“场景”数据集 (27x),其中 A、B 和 C 是模型中的某些输入值,值是变量的结果。

现在我想用 ggplot 制作一个分组条形图(y 上的值,x 上的因子 B,由 A 填充。我想根据因子 C 引起的变化制作误差条。

我的数据集(简化)大致采用以下格式:

data <- data.frame(matrix(ncol=0, nrow=27))
data$value <- runif(27, min=10, max=60)
data$A <- factor((rep(1:9, each=3)))
data$B <- factor((rep(1:3, each=9)))
data$C <- factor(rep(rep(1:3),9))

看起来像:

     value A B C
1 27.76710 1 1 1
2 34.71762 1 1 2
3 20.72895 1 1 3
4 34.83710 2 1 1
5 31.44144 2 1 2
6 13.11038 2 1 3
etc

ggplot 是

ggplot(data, aes(fill=A, y=value, x=B)) + 
  geom_bar(stat="identity",position=position_dodge())+
  geom_errorbar(aes(ymin=?????, ymax=????), width=.2,
                position=position_dodge(.9))

所以我在 ymin 和 ymax 上挣扎。它可能是 value+sd 或 -sd,但我还没有计算出 sd。

我现在的方法是使用 A 组的 dplyr 总结。这给了我:

data %>% 
group_by(A) %>% 
summarise(mean=mean(value), sd = sd(value))

  A      mean    sd
  <fct> <dbl> <dbl>
1 1      27.7  6.99
2 2      26.5 11.7 
3 3      33.7 21.9 
4 4      27.7  6.99
etc

这很好,但是,现在我丢失了所有其他列(在这种情况下,我的 ggplot 仍然需要 B)。我如何仍然计算平均值和标准差并保留所有其他列?

或者还有其他方法可以达到我需要的效果吗? (我可以手动重新添加 B 列,但我想知道是否还有其他方法可以用于将来以及 B 不容易重新制作的情况)

最佳答案

对于 AB 的每种组合,您都有三行数据,因此您当前的代码实际上在每个 x 轴位置过度绘制了三个条形。您可以通过向条形图添加透明度来看到这一点。

ggplot(data, aes(fill=A, y=value, x=B)) + 
  geom_bar(stat="identity", position=position_dodge(), alpha=0.3)

enter image description here

看起来您实际上正在尝试执行以下操作(但如果我误解了,请告诉我):

pd = position_dodge(0.92)

data %>% 
  group_by(A,B) %>% 
  summarise(mean=mean(value), sd=sd(value)) %>% 
  ggplot(aes(fill=A, x=B)) + 
  geom_col(aes(y=mean), position=pd)+
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position=pd, width=0.2)

enter image description here

分面是另一种选择:

data %>% 
  group_by(A,B) %>% 
  summarise(mean=mean(value), sd=sd(value)) %>% 
  ggplot(aes(x=A)) + 
    geom_col(aes(y=mean), fill=hcl(240,100,65)) +
    geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=0.2) +
    facet_grid(. ~ B, labeller=label_both, space="free_x", scales="free_x")

enter image description here

但是你真的需要酒吧吗?

data %>% 
  group_by(A,B) %>% 
  summarise(mean=mean(value), sd=sd(value)) %>% 
  ggplot(aes(x=A)) + 
  geom_pointrange(aes(y=mean, ymin=mean-sd, ymax=mean+sd), shape=21, fill="red", 
                  fatten=6, stroke=0.3) +
  facet_grid(. ~ B, labeller=label_both, space="free_x", scales="free_x")

我们还可以在 ggplot 中使用 stat_summary 进行此计算:

data %>% 
  ggplot(aes(x=A, y=value)) + 
  stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom="pointrange", 
               shape=21, fill="red", fatten=6, stroke=0.3) +
  facet_grid(. ~ B, labeller=label_both, space="free_x", scales="free_x")

无论哪种方式,情节都是这样的:

enter image description here

关于r - 如何在 R 中按组计算 SD,而不丢失在 ggplot2 中绘图仍需要的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60934832/

相关文章:

R相当于一个matlab "cell matrix"

使用 dplyr 重新编码多列

r dplyr - 读取文件列表并使用文件名作为变量

r - group_by 和 pmap 对每组的每一行进行分段操作(ifelse 与 case_when)

r - 如何使用 ggplotly 绘制 3D 图形?

r - 根据经度/纬度确定 UTM 区域(要转换)

r - 在 R 中的 H2O 中将两个随机森林模型中的树模型合并为一个随机森林模型

r - 如何更改 R 传单中的图例文本颜色?

r - 如何从 facet_grid() (ggplot2) 中移除灰色背景

r - 使用 coord_cartesian 仅剪辑 1 个轴