Uwe W.
Uwe W.

Reputation: 51

ggplot dodged barplot

I'm desperately looking for a solution for this problem:

I want to create a barplot from the following dataframe:

means <- c(2.4,3,3,3.16,2.5,2.5,3,4.5)
sds <- c(1.0,1.2,1.0,1.1,2.1,0.7,2.8,0.7)
teams <- c(1,1,1,1,2,2,2,2)
scales <- c(1,2,3,4,1,2,3,4)

datas <- data.frame(teams, scales, means, sds)

Before, it worked well and I got a picture which where beside each other in blue and red with standard deviations.

graph <- ggplot(data=datas,  aes(scales, y=means))
graph + 
  geom_bar(aes(fill=teams), stat="identity", position="dodge") + 
  no_margins + 
  geom_errorbar(aes(ymin= means - sds, ymax = means + sds, width=0.2), position=position_dodge(width=0.90)) + 
  coord_flip() 

After installing the latest version of R (2.15.1), all I get is a file, where the 4 bars appear in blue and not dodged (beside each other)

Does anyone have an idea, where the change (colour, dodge) came from?

I have spent hours for results, so thank you very much for any recommendations!

Upvotes: 2

Views: 2999

Answers (1)

Brian Diggs
Brian Diggs

Reputation: 58825

I'm not sure why it isn't working right, but here is a version that does.

graph <- 
    ggplot(data=datas,  aes(scales, y=means, group=teams)) + 
    geom_bar(aes(fill=teams), stat="identity", 
             position="dodge") + 
    geom_errorbar(aes(ymin= means - sds, ymax = means + sds, width=0.2), 
                  position=position_dodge(width=0.90)) +
    coord_flip()

I put group=teams into the aesthetics; it shouldn't be necessary because you have a fill=teams in geom_bar.

enter image description here

Upvotes: 3

Related Questions