两组中r的错误栏ggplot2

时间:2015-12-16 15:32:42

标签: r ggplot2

我想绘制mean(z)/mean(b)的标准偏差,它们按两个因素$ angle和$ treatment分组:

z= Tracer angle treatment 60 0 S 51 0 S 56.415 15 X 56.410 15 X

b=Tracer angle treatment 21 0 S 15 0 S 16.415 15 X 26.410 15 X

enter image description here 到目前为止,我已经根据角度和处理计算了每个变量的平均值:

aggmeanz <-aggregate(z$Tracer, list(angle=z$angle,treatment=z$treatment), FUN=mean) aggmeanb <-aggregate(b$Tracer, list(angle=b$angle,treatment=b$treatment), FUN=mean)

现在看起来像这样: aggmeanz angle treatment x 1 0 S 0.09088021 2 30 S 0.18463353 3 60 S 0.08784315 4 80 S 0.09127198 5 90 S 0.12679296 6 0 X 2.68670392 7 15 X 0.50440692 8 30 X 0.83564470 9 60 X 0.52856956 10 80 X 0.63220093 11 90 X 1.70123025

但是当我来绘制它时,我不能完全得到我想要的东西

ggplot(aggmeanz, aes(x=aggmeanz$angle,y=aggmeanz$x/aggmeanb$x, colour=treatment)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=0.1, ymax=1.15),
                width=.2,                    
                position=position_dodge(.9))  +
  theme(panel.grid.minor = element_blank()) +
  theme_bw()

修改

dput(aggmeanz)
structure(list(time = structure(c(1L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L), .Label = c("0", "15", "30", "60", "80", "90"
), class = "factor"), treatment = structure(c(1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("S", "X"), class = "factor"), 
    x = c(56.0841582902523, 61.2014237854156, 42.9900742785269, 
    42.4688447229277, 41.3354173870287, 45.7164231791512, 55.3943182966382, 
    55.0574951462903, 48.1575625699563, 60.5527200655174, 45.8412287451211
    )), .Names = c("time", "treatment", "x"), row.names = c(NA, 
-11L), class = "data.frame")

> dput(aggmeanb)
structure(list(time = structure(c(1L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L), .Label = c("0", "15", "30", "60", "80", "90"
), class = "factor"), treatment = structure(c(1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("S", "X"), class = "factor"), 
    x = c(56.26325504249, 61.751655279608, 43.1687113436753, 
    43.4147408285209, 41.9113698082799, 46.2800894420131, 55.1550995335947, 
    54.7531592595068, 47.3280215294235, 62.4629068516043, 44.2590192583692
    )), .Names = c("time", "treatment", "x"), row.names = c(NA, 
-11L), class = "data.frame")

编辑2:我按如下方式计算了标准开发区:

aggstdevz <-aggregate(z$Tracer, list(angle=z$angle,treatment=z$treatment), FUN=std) aggstdevb <-aggregate(b$Tracer, list(angle=b$angle,treatment=b$treatment), FUN=std)

任何想法都会非常感激,

干杯

2 个答案:

答案 0 :(得分:3)

正如其他人所说,您需要将两个数据帧连接在一起。您展示的dput数据中也存在一些小怪癖,因此我重新命名了一些列以确保它们正确连接并匹配您尝试过的内容。注意:您需要以不同方式命名这两种方式,以免它们合并在一起或引起冲突。

names(aggmeanb)[names(aggmeanb) == "x"] = "mean_b"
names(aggmeanb)[names(aggmeanb) == "time"] = "angle"
names(aggmeanz)[names(aggmeanz) == "x"] = "mean_z"
names(aggmeanz)[names(aggmeanz) == "time"] = "angle"

joined_data = join(aggmeanb, aggmeanz)
joined_data$divmean = joined_data$mean_b/joined_data$mean_z

> head(joined_data)
  angle treatment   mean_b   mean_z divmean
1     0         S 56.26326 56.08416 1.003193
2    30         S 61.75166 61.20142 1.008991
3    60         S 43.16871 42.99007 1.004155
4    80         S 43.41474 42.46884 1.022273
5    90         S 41.91137 41.33542 1.013934
6     0         X 46.28009 45.71642 1.012330


ggplot(joined_data, aes(factor(angle), divmean)) + 
  geom_boxplot() + 
  theme(panel.grid.minor = element_blank()) +
  theme_bw()

enter image description here

您所包含的数据可能仅仅是您的实际数据集的一部分,但每个角度处理组只有一个数据点。但是,当您使用更全面的数据集时,可以尝试类似:

ggplot(joined_data, aes(factor(angle), diffmean, group = treatment)) + 
  geom_boxplot() + 
  facet_grid(.~angle, scales = "free_x")

这将按角度对方框进行分组,然后让您通过处理填充它们。

enter image description here

答案 1 :(得分:2)

分两步思考问题:

  1. 创建一个包含所有信息的数据框(比如data) 你想要想象。在这种情况下,这似乎是两个 因素(angletreatment),平均组差异(比如dif) 和标准错误(比如ste)。
  2. 可视化此信息。
  3. 步骤2)将很容易。这可能会产生与草图非常相似的东西。

    ggplot(data, aes(x=angle, y=dif, colour=treatment)) + 
         geom_point(position=position_dodge(0.1)) + 
         geom_errorbar(aes(ymin=dif-ste, ymax=dif+ste), width=.1, position=position_dodge(0.1)) + 
         theme_bw()
    

    但是,此时,您没有提供足够的信息来获取步骤1的帮助。尝试包含生成原始数据的代码(或您拥有的数据类型),而不是复制粘贴数据输出的块或粘贴缺少标准错误的汇总数据。

    组合两个聚合数据帧并为标准错误生成随机数,产生下图:

    #I imported your two aggregated data frames from your dput output.
    data <- cbind(aggmeanb, aggmeanz$x, rnorm(11))
    names(data) <- c("angle", "treatment", "meanz", "meanb", "ste")
    data$dif <- data$meanz - data$meanb 
    

    plot demonstration with randomly generated confidence intervals