geom_bar position="dodge" does not dodge

419 views
Skip to first unread message

Julius

unread,
Jun 14, 2012, 6:58:28 PM6/14/12
to ggp...@googlegroups.com
Hi everyone,

I posted this in another forum but I cannot get the answers or results that I wanted. I have a dataframe:

p3 <- structure(list(test = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5),
result = c(26.88,24.53,24.02,20.33,22.01,19.84, 19.69,19.84,19.24,19.24,34.36,33.28,33.82,33.82,32.47,25.55,26.90,25.40,24.20,25.85,25.70,24.95,18.65,18.65,17.80,18.65,18.31)
), .Names = c("test", "result"), row.names = c(NA, -27L), class = "data.frame")

I wanted to make a 'dodged' barplot with 'test' in the x-axis and 'result' in the y-axis using the following:

ggplot(p3, aes(x = test, y = result))+ geom_bar(position="dodge", stat="identity")

Somehow I could not make it work. I have been using this code before but ever since I upgraded to the latest ggplot2 package, it was not behaving well. Can someone help me to fix this?


Cheers,



Kohske Takahashi

unread,
Jun 14, 2012, 7:13:07 PM6/14/12
to Julius, ggp...@googlegroups.com
Hi

It looks working well.
What kinds of result do you expect?
Could you provide a sample figure?

2012/6/15 Julius <jute...@yahoo.com>:
> ggplot(p3, aes(x = test, y = result))+ geom_bar(position="dodge",
> stat="identity")



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Dennis Murphy

unread,
Jun 14, 2012, 11:27:19 PM6/14/12
to Julius, ggp...@googlegroups.com
Hi:

Were you looking for something like this?

p3$test <- factor(p3$test)
p3$fac <- factor(unlist(sapply(as.vector(table(p3$test)), seq_len)))

ggplot(p3, aes(x = test, y = result, fill = fac)) +
geom_bar(position = 'dodge', stat = 'identity')

If you want to dodge a factor, you need to tell ggplot2 which factor to dodge :)

HTH,
Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Julius Tesoro

unread,
Jun 15, 2012, 3:33:16 AM6/15/12
to Dennis Murphy, ggp...@googlegroups.com
This works! Thank you. The following code shows what I really wanted. But how do I make all the colors similar to the fill(darkgreen)?
ggplot(p3, aes(x = test, y = result, color = fac)) + geom_bar(position = 'dodge', stat = 'identity', fill = 'darkgreen')

Brandon Hurr

unread,
Jun 15, 2012, 5:55:47 AM6/15/12
to Julius Tesoro, Dennis Murphy, ggp...@googlegroups.com
What do you mean by "similar"? 
You could use the green palette in scale_brewer, but the lighter end is very white. If you want dark colors you can choose from lots. Baptiste made a couple of really good posts on colors in ggplot2 on the ggplot2 blog. 

#Greens
ggplot(p3, aes(x = test, y = result, fill = fac)) + geom_bar(position = 'dodge', stat = 'identity')+scale_fill_brewer(palette="Greens")

#dark2 palette
ggplot(p3, aes(x = test, y = result, fill = fac)) + geom_bar(position = 'dodge', stat = 'identity')+scale_fill_brewer(palette="Dark2")

HTH

On Fri, Jun 15, 2012 at 8:33 AM, Julius Tesoro <jute...@yahoo.com> wrote:
This works! Thank you. The following code shows what I really wanted. But how do I make all the colors similar to the fill(darkgreen)?
ggplot(p3, aes(x = test, y = result, color = fac)) + geom_bar(position = 'dodge', stat = 'identity', fill = 'darkgreen')

--
Reply all
Reply to author
Forward
0 new messages