使用ggplot2绘制一个带有多个因子并明确定义标准偏差的漂亮箱图

时间:2015-02-07 11:54:12

标签: r ggplot2 statistics

我有一个数据框,其中只包含每个组的平均值,并使用显式数字定义每个组的标准偏差(上限和下限)。这是数据框:

 dat <- read.table(text = '
   mean dist      group     lower     upper rev.lower   factor
 -4.058047    0    P -4.096849 -4.019245 -4.096849  dd
 -4.001524    0 Ps -4.040326 -3.962722 -4.040326  dd
 -3.956972    0    P -4.028551 -3.885393 -4.028551  tt
 -4.050114    0 Ps -4.121694 -3.978535 -4.121694  tt
 -2.859591    0    P -2.955156 -2.764026 -2.955156 aa1
 -3.017559    0 Ps -3.113124 -2.921994 -3.113124 aa1
 -3.094085    0    P -3.196434 -2.991736 -3.196434 aa4
 -3.265121    0 Ps -3.367470 -3.162772 -3.367470 aa4
 -4.283909    0    P -4.352606 -4.215213 -4.352606  ba1
 -4.344173    0 Ps -4.412870 -4.275477 -4.412870  ba1
 -4.594736    0    P -4.664228 -4.525243 -4.664228  bh2
 -4.545442    0 Ps -4.614935 -4.475950 -4.614935  bh2
 -4.843653    0    P -4.925350 -4.761957 -4.925350 g6
 -4.492781    0 Ps -4.574478 -4.411085 -4.574478 g6
 -4.072374    0    P -4.205603 -3.939145 -4.205603 gx
 -3.742987    0 Ps -3.876216 -3.609758 -3.876216 gx',head=T, fill=T)

我试过了:

ggplot(dat, aes(factor(factor), value)) + geom_boxplot(aes(fill=factor(group)))

它可以工作,但我不确定如何绘制较低和较高的SD

每个组的每个因素。请告知我如何绘制图表...

非常感谢

1 个答案:

答案 0 :(得分:1)

尝试添加

your_barplot + geom_errorbar(aes(ymax=upper,
                              ymin=lower),
                          position=position_dodge(0.9),
                          data=dat)