![](/img/trans.png)
[英]Adding error bars from different columns in grouped barplot with ggplot2
[英]Multiple error bars add to columns in error on barplot in ggplot2
我只是想使用ggplot將誤差線添加到並排柱線圖中。 我認為數據如下正確排列。 我想畫出有(+ 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
我用於繪制情節的代碼是
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))
一般從R菜譜網站來到這里
似乎發生的情況是,為我的分組變量(近交)的每組添加了每列/條內的多個錯誤條(我認為)。 抱歉,我的聲譽不夠高,無法添加圖片。 所以我希望代碼清晰。 我的目標是R Cookbook網站上的並排條形圖,但我在x軸上的變量(SS)只有兩組。
顯然,我只想在情節上每條線有一個誤差線。 如果有人可以建議我在這里做錯了,我將不勝感激。
與R cookbook中的示例相比,您的數據具有重復的條目,即,每個SS和近交組合不是唯一的,因為您每個都有四個。 第一欄中的數值是多少? 它是否類似於第二個治療變量,即您是否期望有16個柱形? 然后,您將需要查找以下內容:
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
如果僅查找四個條,則數據集應僅包含4行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.