0

I am plotting grouped barplots with error bars, but my error bars are very long as in this image

[![https://i.stack.imgur.com/VUByO.png][1]][1].

I would like shorter error bars as in this image

[![https://i.stack.imgur.com/JhaUJ.png][2]][2]

The code used

per$Leaf_Location <- factor(per$Leaf_Location, levels = unique(per$Leaf_Location))
per$Time <- factor(per$Time, levels = unique(per$Time))
ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
stat_summary(fun.y=mean, 
geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + 
stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
color="black",position=position_dodge(.7), width=.2) +
stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7), 
label = c("a","b","c","d","d","a","b","c","d","d","a","b","c","d","d"), vjust = -0.5) +
scale_fill_manual("Legend", values = c("grey36","grey46","grey56","grey76","grey86","grey96")) + 
 xlab("Leaf Location") +
ylab("Damage ") +
theme_bw()

data:

per =
structure(list(Site = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 
    2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("20_days", 
    "40_days", "60_days", "80_days", "100_days"), class = "factor"), 
    Damage = c(25.25, 26.07, 24.43, 20.73, 17.8, 6.9, 45.05, 
    33.47, 24.43, 51.67, 41.72, 34.17, 81.67, 73.33, 55.83, 34.28, 
    26.08, 13.28, 26.27, 14.1, 6.93, 37.55, 29.33, 23.62, 49.17, 
    38.45, 31.38, 70.83, 60.83, 44.2, 31.03, 25.2, 14.97, 14.38, 
    6.5, 4.33, 52.2, 39.17, 30.97, 75, 62.5, 38.33, 87.5, 62.5, 
    57.5, 45.02, 31.02, 26.07, 46.72, 34.32, 21.5, 50.83, 34.23, 
    25.25, 45.83, 33.47, 27.7, 67.67, 57.5, 52.67, 30.98, 23.62, 
    9.1, 18.17, 18.57, 10.15, 46.67, 34.27, 23.62, 54.17, 40.05, 
    29.37, 70.83, 59.17, 47.53, 8.67, 5.63, 0.87, 9.87, 3.03, 
    0, 17.75, 6.88, 0, 62.5, 37.5, 27.7, 70.83, 57.5, 50.83, 
    6.5, 2.17, 1.3, 6.93, 3.03, 0.53, 14.82, 5.2, 0, 37.5, 28.52, 
    13, 75, 37.5, 37.5, 15.3, 9.53, 5.63, 9.43, 3.03, 0.43, 16.4, 
    6.07, 0, 57.5, 34.23, 21.98, 78.33, 62.5, 37.5, 12.08, 6.5, 
    1.3, 10.73, 3.03, 0, 15.2, 3.9, 0.43, 62.5, 37.5, 21.98, 
    64.17, 55.83, 41.73, 8.73, 3.57, 0, 8.57, 2.17, 0, 16.5, 
    7.7, 0.43, 42.58, 36.68, 13, 65.83, 47.5, 37.5, 8.03, 5.07, 
    0.43, 10.68, 7.27, 3.5, 48.38, 38.42, 24.83, 45.03, 38.4, 
    30.8, 73.33, 63.33, 50.83, 3.37, 2.17, 0.9, 9, 6.02, 5.2, 
    21.07, 12.37, 6.02, 45.02, 32.65, 21.67, 68.78, 56.68, 50, 
    0, 0, 0, 7.8, 4.33, 4.33, 25.17, 20.65, 13.15, 48.37, 39.23, 
    27.17, 75.83, 62.5, 49, 11.78, 12.72, 3.8, 20.18, 14.87, 
    8.95, 46.7, 39.32, 33.03, 49.18, 40.05, 24.43, 69.17, 60, 
    48.33, 0, 0, 0, 15.25, 9.82, 7.75, 45.9, 38.47, 35.52, 50.88, 
    37.61, 33.47, 79.17, 71.67, 58.33)), .Names = c("Site", "Field", 
"Leaf_Location", "Time", "Damage"), row.names = c(NA, -225L), class = "data.frame")
dww
  • 30,425
  • 5
  • 68
  • 111
LEYLA_M
  • 11
  • 1
  • 1
    [Please provide](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) some or all of the data in `per` as plain text. Also check your data: why do you think the error bars are too long? – neilfws Mar 10 '19 at 22:46
  • 1
    I have edited my question as required, @neilfws – LEYLA_M Mar 11 '19 at 00:04

1 Answers1

0

Here's a simplified reproducible example to explain

first, some dummy data:

per = data.frame(x=rep(c('a','b'), each=100), y=c(2+rnorm(100), 3+rnorm(100,0,2)))

Now you are plotting the error bars, using fun.ymin=min, fun.ymax=max, which will cause them to extend the full range of the data, as in the following graph:

ggplot(per, aes(x, y)) +
  stat_summary(fun.y = mean, geom="bar") + 
  geom_point(position = position_jitter(0.1)) +
  stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar", width=0.4) +
  theme_bw()

enter image description here

Whereas, it is more conventional to use error bars that extend either +/- one standard deviation, as in the following:

ggplot(per, aes(x, y)) +
  stat_summary(fun.y = mean, geom="bar") + 
  stat_summary(
    fun.ymin=function(y) {mean(y) - sd(y)}, 
    fun.ymax=function(y) {mean(y) + sd(y)}, 
    geom="errorbar", width=0.2) +
  theme_bw()

enter image description here

Or one standard error, like this:

ggplot(per, aes(x, y)) +
  stat_summary(fun.y = mean, geom="bar") + 
  stat_summary(
    fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))}, 
    fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))}, 
    geom="errorbar", width=0.2) +
  theme_bw()

enter image description here

EDIT - example data were added to question, after this answer was originally posted

We can applying exactly the same approach as above to your example data:

ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
  stat_summary(fun.y=mean, geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + 
  stat_summary(
    fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))}, 
    fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))}, 
    geom="errorbar",
    position=position_dodge(.7), width=.2)

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111
  • I have tried the suggestion of using the standard errors but i have not yet achieve the desired result. Can you plot using the sample data i have provided @dww – LEYLA_M Mar 11 '19 at 08:05
  • There is no difference from my example to use this on your data. I demonstrated for you. – dww Mar 11 '19 at 12:05
  • Glad to help. Please feel free to show your appreciation by accepting this answer (the green check mark), and upvoting it. – dww Mar 11 '19 at 21:18