简体   繁体   English

ggplot2的barplot上有多个错误栏添加到错误的列

[英]Multiple error bars add to columns in error on barplot in ggplot2

I am simply trying to add error bars to a side by side bar plot using ggplot. 我只是想使用ggplot将误差线添加到并排柱线图中。 I think data is arranged correctly as per below. 我认为数据如下正确排列。 I want to graph differences in fecundity between inbred and outbred populations derived from populations with (+SS) and without (-SS) sexual selection. 我想画出有(+ SS)和没有(-SS)性选择的群体的近交和近交群体的繁殖力差异。

       inbreeding  SS       Fecundity      se
1      Inbred      +SS      5.60      0.8596205 
2      Inbred      +SS      7.40      1.1639316 
3      Inbred      +SS      6.25      1.2457824 
4      Inbred      +SS      1.40      0.1854050 
5     Outbred      +SS      7.70      1.2377824 
6     Outbred      +SS      6.30      0.6613384 
7     Outbred      +SS      2.35      1.0137865 
8     Outbred      +SS      8.27      1.2775966 
9      Inbred      -SS      9.15      1.7595977 
10     Inbred      -SS     12.50      1.7464249 
11     Inbred      -SS     10.95      1.9063260 
12     Inbred      -SS      3.65      1.2036676 
13    Outbred      -SS      7.65      1.5564382 
14    Outbred      -SS      9.10      1.5250539 
15    Outbred      -SS      5.75      1.3315503
16    Outbred      -SS      3.65      0.9821432

The code I am using for making the plot is 我用于绘制情节的代码是

ggplot(Inbreeding27Means.ggplot,aes(x=SS,y=Fecundity,fill=inbreeding))+
geom_bar(stat="identity",position=position_dodge())+
geom_errorbar(aes(ymin=Fecundity-se,ymax=Fecundity+se),width=.2,
  position=position_dodge(.9))

Which generally came from the R cookbook website here 一般从R菜谱网站来到这里

What appears to happen is that multiple error bars within each column/bar are added for each group of my grouping variable (inbreeding) ( I think). 似乎发生的情况是,为我的分组变量(近交)的每组添加了每列/条内的多个错误条(我认为)。 Sorry my reputation is not quiet high enough to add a picture. 抱歉,我的声誉不够高,无法添加图片。 So I hope code is clear. 所以我希望代码清晰。 The side by side bar graphs on the R cookbook website is essentially what I am aiming for except my variable (SS) on x-axis has only two groups. 我的目标是R Cookbook网站上的并排条形图,但我在x轴上的变量(SS)只有两组。

Obviously I'd like to just have one error bar per bar on the plot. 显然,我只想在情节上每条线有一个误差线。 If someone could suggest what I may have done wrong here i'd much appreciate it. 如果有人可以建议我在这里做错了,我将不胜感激。

Comparing with the example from R cookbook, your data has duplicate entries, ie each SS and inbreeding combination is not unique as you have four each. 与R cookbook中的示例相比,您的数据具有重复的条目,即,每个SS和近交组合不是唯一的,因为您每个都有四个。 What is the numeric value in the first column? 第一栏中的数值是多少? Is it something like a second treatment variable, ie do you expect there to be 16 bars? 它是否类似于第二个治疗变量,即您是否期望有16个柱形? Then you would want to look for something like this: 然后,您将需要查找以下内容:

data$population = c(1:16)
data$treatment = paste(data$population,data$inbreeding)

g=ggplot(data,aes(x=as.factor(population),y=Fecundity,fill=as.factor(SS),group=inbreeding))
g=g+geom_bar(stat="identity",position=position_dodge())
g=g+geom_errorbar(aes(ymin=Fecundity-se,ymax=Fecundity+se),width=.2,position=position_dodge(.9))
g=g+facet_grid(.~inbreeding,scales="free_x")
g

16条带误差线

If you are only looking for four bars your data set should only have 4 rows. 如果仅查找四个条,则数据集应仅包含4行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM