2013-04-15 81 views
0

這裏我的數據幀如何GGPLOT2

Month foresttype type average sd 
May. LYL branch 9.18 6.21 
May. LYL leaf 2.41 0.63 
May. LYL flower 5.48 4.83 
May. LYL barks 0.56 0.17 
May. HBS branch 6.38 1.14 
May. HBS leaf 6.81 4.48 
May. HBS flower 2.55 1.38 
May. HBS barks 0.48 0.17 
May. YSL branch 0.9 0.18 
May. YSL leaf 10.38 8.18 
May. YSL flower 10.19 3.81 
May. YSL barks 0.39 0.14 
Jun. LYL branch 10.17 7.62 
Jun. LYL leaf 1.99 1.33 
Jun. LYL flower 0.4 0.26 
Jun. LYL barks 0.16 0.13 
Jun. HBS branch 9.81 8.79 
Jun. HBS leaf 3.02 0.41 
Jun. HBS flower 3.41 4.93 
Jun. HBS barks 0.62 0.6 
Jun. YSL branch 1.39 0.26 
Jun. YSL leaf 4.39 5.26 
Jun. YSL flower 10.67 10.39 
Jun. YSL barks 0.33 0.03 
Jul. LYL branch 3.23 3.99 
Jul. LYL leaf 1.21 0.09 
Jul. LYL flower 1.74 1.04 
Jul. LYL barks 0.23 0.25 
Jul. HBS branch 2.73 3.11 
Jul. HBS leaf 3.46 2.06 
Jul. HBS flower 3.31 4.93 
Jul. HBS barks 0.7 0.38 
Jul. YSL branch 1.38 0.84 
Jul. YSL leaf 3.4 4.44 
Jul. YSL flower 10.9 9.81 
Jul. YSL barks 0.66 0.5 
Jul. LYL branch 1.54 1.7 
Aug. LYL leaf 2.01 0.75 
Aug. LYL flower 2.61 0.77 
Aug. LYL barks 0.3 0.51 
Aug. HBS branch 0.22 0.37 
Aug. HBS leaf 9.25 9.87 
Aug. HBS flower 2.58 1.61 
Aug. HBS barks 0.14 0.1 
Aug. YSL branch 0.8 0.52 
Aug. YSL leaf 1.86 0.65 
Aug. YSL flower 4.92 4.45 
Aug. YSL barks 0.71 0.35 
Sep. LYL branch 8.92 3.25 
Sep. LYL leaf 42.16 29.3 
Sep. LYL flower 17.38 12.96 
Sep. LYL barks 0.16 0.04 
Sep. HBS branch 2.9 2.74 
Sep. HBS leaf 52.47 42.82 
Sep. HBS flower 3.02 2.29 
Sep. HBS barks 0.1 0.1 
Sep. YSL branch 4.58 1.5 
Sep. YSL leaf 17.25 17.17 
Sep. YSL flower 5.15 4.06 
Sep. YSL barks 0.14 0.06 
Oct. LYL branch 3.18 2.13 
Oct. LYL leaf 65.74 59.98 
Oct. LYL flower 0.69 0.41 
Oct. LYL barks 0.14 0.12 
Oct. HBS branch 0.1 0.1 
Oct. HBS leaf 60.08 62.02 
Oct. HBS flower 0.3 0.28 
Oct. HBS barks 0.04 0.04 
Oct. YSL branch 1.21 1.15 
Oct. YSL leaf 41.54 25.02 
Oct. YSL flower 2.75 2.06 
Oct. YSL barks 0.14 0.09 


df <- ddply(data1,.(Month,foresttype),transform,ystart = cumsum(average),yend = cumsum(average) + sd) 

p=ggplot() +geom_bar(data=df, aes(y = average, x = foresttype, fill = type),stat="identity",position='stack') +theme_bw() + facet_grid(~ Month) 

m=p+geom_segment(data=df,aes(x=foresttype,xend=foresttype,ymin = ystart,ymax = yend))+theme_bw() 

n=m+geom_point(data=df,aes(x = foresttype,y = yend),shape = "|",show_guide = FALSE) 

添加errorbar現在,我不知道爲什麼errorbar不能加入?

回答

3

您可以使用geom_errorbar()添加誤差線(參數width=0只會生成沒有水平線的垂直線)。通過將dfx=foresttype放在ggplot()調用中,簡化了代碼,因爲它在所有geoms中都使用。

ggplot(df,aes(x = foresttype))+ 
    geom_bar(aes(y = average, fill = type),stat="identity",position='stack')+ 
    geom_errorbar(aes(ymin = ystart,ymax = yend),width=0)+facet_grid(~ Month) 

enter image description here

+0

非常感謝!我有一個新的問題,如何根據每月訂購了吧,你看上面的圖,是八月第一棒,但我想打月。作爲第一,以及如何做? – user2282593

+0

@ user2282593您應該重新排列事實的級別,然後繪製數據框中的月份df $ Month <-factor(df $ Month,levels = c(「May。」,「Jun。」,「Jul。」,「Aug。 」, 「SEP」, 「十月」)) –