Problem placing error bars at the center of the columns in ggplot()
Asked Answered
F

2

7

I am having a problem with my bar chart- the error bars just appear on the corner of the columns of the grouping variable rather than on them in a centralised way. The code I am using is this:

a <- data.frame (Cond = c("In", "In", "Out", "Out"),
                Temp = c("Hot", "Cool", "Hot", "Cool"),
                Score = c(.03, -.15, 0.84, 0.25),
                SE = c(.02, .08, .14, .12))
a.bar <- ggplot (data = a, aes(x = Cond, y = Score, fill = Temp)) +
            theme_bw() + theme(panel.grid = element_blank ()) +
            coord_cartesian (ylim = c(-0.5, 1)) + 
            geom_bar (aes(fill = Temp), stat = "identity", position = "dodge", width = .5) +
            geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.9), width = .08) +
            labs(y = "Scores" , x = "Cond") +
            scale_y_continuous (breaks = pretty_breaks(n=8)) +
            theme(legend.title = element_blank()) +
            theme(legend.position = "right")

The alternative codes I have tried, which I couldn't get to work either, included adding "show.legend = FALSE" to geom_bar(); adding "facet_wrap(~Cond)" plot.a; and introducing "fill = Temp" within ggplot(aes()). The closest solution was when I changed the position_dodge() argument into:

geom_bar (aes(fill = Temp), stat = "identity", position = position_dodge(width = .5)) +
geom_errorbar (aes(ymin = Score - SE, ymax = Score + SE, group = Cond), position = position_dodge(.5), width = .08) +

(the rest of the code remains the same). This moved the error bars towards the center of the columns, yet also moved the columns towards each other, eventually making them overlap (see attached figure). see attached figure

I would very much appreciate help on this.

Thanks!

Fortunna answered 16/2, 2016 at 1:40 Comment(0)
I
15

Nice question. A couple of comments:

  1. In general, it's a good practice to set all your aesthetics in the original ggplot() call, and to override them with different aesthetics only if needed in the individual geom_xyz() calls. In your code, you were setting the fill aesthetic twice, once in ggplot and once in geom_bar. You also set the group aesthetic in geom_errorbar(). I don't think any of those things were the ultimate problem, but they did make it harder to debug your code.

  2. The main problem was that the width argument in geom_bar has to match the position_dodge() argument inside geom_errorbar. So if you have

    # ...
    geom_bar(stat = "identity", position = "dodge", width = 0.5)
    # ...
    

    Then you've got to make sure your geom_errorbar() looks like

    # ...
    geom_errorbar(width = .08, position = position_dodge(0.5)) 
    # ...
    

Putting it all together:

require(ggplot2)
require(scales)

# define data
a <- data.frame (Cond = c("In", "In", "Out", "Out"),
                Temp = c("Hot", "Cool", "Hot", "Cool"),
                Score = c(.03, -.15, 0.84, 0.25),
                SE = c(.02, .08, .14, .12))

# return plot with everything except error bars
a.bar <- ggplot (data = a, aes(x = Cond, 
                               y = Score, 
                               fill = Temp,
                               ymin = Score - SE,
                               ymax = Score + SE)) +
            theme_bw() + 
            theme(panel.grid = element_blank ()) +
            coord_cartesian(ylim = c(-0.5, 1)) + 
            # manually setting the width means we will have to tell geom_errorbar() about the new width
            geom_bar(stat = "identity", position = "dodge", width = 0.5) + 
            labs(y = "Scores", x = "Cond") +
            scale_y_continuous(breaks = pretty_breaks(n = 8)) +
            theme(legend.title = element_blank()) +
            theme(legend.position = "right")

# show plot w/ errorbars, note that argument to position_dodge is same as width supplied above
a.bar + geom_errorbar(width = .08, position = position_dodge(0.5)) 

# save results
ggsave('SO_35424162.png')

Final graph

Inattentive answered 16/2, 2016 at 5:28 Comment(0)
S
1

I would like to add, since I ran into the same issue:

It is very important to specify the fill parameter in the main aes() and not in the geom_barplot.

Sade answered 28/2, 2019 at 16:30 Comment(1)
This is incredibly important! I couldn't debug this for a long time (because I thought that fill should be irrelevant), but this is crucial and not super intuitive.Vicarious

© 2022 - 2024 — McMap. All rights reserved.