tags:

views:

42

answers:

1

I have a fairly simple data.frame dput(x), below:

x <- structure(list(variable = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j", "k", "l", "m", "n", "o", "p"), top2 = c(0.51, 
0.24, 0.55, 0.36, 0.56, 0.67, 0.36, 0.22, 0.48, 0.6, 0.59, 0.06, 
0.04, 0.2, 0.26, 0.25), bottom = c(0.03, 0.05, 0.03, 0.01, 0.02, 
0.03, 0.01, 0.05, 0.03, 0, 0.03, 0.2, 0.11, 0.06, 0.16, 0.07)), .Names = c("variable", 
"top2", "bottom"), row.names = c(NA, -16L), class = "data.frame")

What I'd like to do is create a graph that shows top2 and bottom beside each other for each "variable". I'm trying to implement position="dodge" but so far it seems to be largely ignored by ggplot2

ggplot() + geom_bar(aes(variable, top2), data=x, position="dodge") +
geom_bar(aes(x$variable, x$bottom), data=x, position="dodge", fill="pink") + 
coord_flip()

The pink isn't staying :P

+1  A: 

here is a quick answer

df = melt(x, id = 'variable');
names(df) = c('variable', 'topbot', 'value');
pl1 = ggplot(df, aes(x = variable, y = value)) +
      geom_bar(aes(fill = topbot, position = 'dodge')) +
      coord_flip() +
      scale_fill_manual(values = c('red', 'blue'));
print(pl1)
Ramnath
That one doesn't work. a) It overplots. b) I'm not even sure it's plotting the right values.
Brandon Bertelsen
ah, it works if you add ', position="dodge"' in geom_bar() thanks!
Brandon Bertelsen
oh right. i forgot to include it in the code i posted. glad that you figured it out!
Ramnath
Could you edit the post so a passerby get's the right answer? :)
Brandon Bertelsen
i have edited the post!!
Ramnath