0

Here my data frame

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) 

Now, I do not know why errorbar cannot be added?

1 Answer 1

3

You can add error bars with geom_errorbar() (arguments width=0 will make just vertical lines without horizontal line). Also simplified your code by putting df and x=foresttype inside ggplot() call as it is used in all 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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot!i have a new questions,how to order the bar according to the Month, as you see above figure,Aug is first bar,but i want to make May. as the first, and how to do?
@user2282593 You should reorder levels of facts Month in dataframe df and then plot data df$Month<-factor(df$Month,levels=c("May.","Jun.","Jul.","Aug.","Sep.","Oct."))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.