Re: sorting and ordering bars in a ggplot2 bar chart.

6,687 views
Skip to first unread message

Ben Bond-Lamberty

unread,
Dec 10, 2012, 10:03:29 AM12/10/12
to ggp...@googlegroups.com
I agree with the stackoverflow responders that the question isn't
completely clear. Nonetheless something like this may help...it
produces an order bar graph by your domain and category variables:

# 1. Summarize the data
library(plyr)
d2 <- ddply(ex1.dta,.(category,domain),summarise,grouping=sum(grouping))

# 2. Reorder and create a new x axis label
d3 <- d2[order(d2$category,d2$grouping),] # reorder
d3$catdom <- paste(d3$category,d3$domain) # make a new plotting var
d3$catdom <- factor(d3$catdom,levels=d3$catdom)

# 3. Plot
qplot(catdom,grouping,fill=category,data=d3,geom="bar",stat="identity")+theme(axis.text.x
= element_text(angle = 90))

Ben


On Mon, Dec 10, 2012 at 7:58 AM, eastafri <geor...@gmail.com> wrote:
> I had posted this question on stackoverflow though i did not get a
> satisfactory answer on how to order the bars using code. Maybe someone has
> an idea?
> http://stackoverflow.com/questions/12449924/grouping-bars-in-a-ggplot2
>
> --
> 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

eastafri

unread,
Dec 12, 2012, 3:00:20 AM12/12/12
to ggp...@googlegroups.com
Thanks Ben. what I wanted are the stacked bars like the ones from the question. Sorry if the initial question was not as clear. But to clarify, I wanted a stacked bar chart with the bars along the X-axis ordered  via some other variable and in an descending manner. I thought it should be straighforward to do this with ggplot2, but it seems to be difficult. 

Dennis Murphy

unread,
Dec 12, 2012, 3:28:07 AM12/12/12
to eastafri, ggp...@googlegroups.com
Hi:

On Wed, Dec 12, 2012 at 12:00 AM, eastafri <geor...@gmail.com> wrote:
> Thanks Ben. what I wanted are the stacked bars like the ones from the
> question. Sorry if the initial question was not as clear. But to clarify, I
> wanted a stacked bar chart with the bars along the X-axis ordered via some
> other variable and in an descending manner. I thought it should be
> straighforward to do this with ggplot2, but it seems to be difficult.

See the reorder() function to arrange the levels of a factor according
to some function of the values of another variable. Here's a simple
reproducible example to illustrate the point:

DF <- data.frame(expand.grid(a = c("I", "II", "III"), b = LETTERS[1:3]),
n = rpois(9, 60))
require(ggplot2)

# ascending order of cumulative bar height:
ggplot(DF, aes(x = reorder(a, n, sum), y = n, fill = b)) +
geom_bar(stat = "identity", position = "stack")

# descending order:
ggplot(DF, aes(x = reorder(a, -n, sum), y = n, fill = b)) +
geom_bar(stat = "identity", position = "stack")

Dennis

George Githinji

unread,
Dec 12, 2012, 3:42:05 AM12/12/12
to Dennis Murphy, ggp...@googlegroups.com
Thanks Dennis,
I am a little confused here. :)
Can you do that with the example I have given and express the y axis
as a proportion of the total.
In my example(http://stackoverflow.com/questions/12449924/grouping-bars-in-a-ggplot2)
all the variables are categorical variables.

Furthermore I get this with your example, when I attempt to use
proportions on the y- axis
ggplot(DF, aes(x = reorder(a, -n, sum), y = n, fill = b)) +
+ geom_bar(position="fill")

Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of
cases in each group.
This can result in unexpected behavior and will not be allowed in a
future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't
map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)

On Wed, Dec 12, 2012 at 11:28 AM, Dennis Murphy <djm...@gmail.com> wrote:
> ggplot(DF, aes(x = reorder(a, -n, sum), y = n, fill = b)) +
> geom_bar(stat = "identity", position = "stack")



--
---------------
Sincerely
George
Skype: george_g2
Blog: http://www.biorelated.com/
Twitter: http://twitter.com/#!/george_l

Ben Bond-Lamberty

unread,
Dec 12, 2012, 7:03:18 AM12/12/12
to ggp...@googlegroups.com
Well, did you try the example code I sent? It orders X axis bars and
should be straightforward to modify for your needs.

Kind regards
Ben

eastafri

unread,
Dec 13, 2012, 5:38:43 AM12/13/12
to ggp...@googlegroups.com
Yes i tried it with 
ggplot(d3,aes(domain,fill=category)) + geom_bar(position="fill") +opts(
+     axis.text.x=theme_text(angle=-90,hjust=0),
+     axis.line = theme_segment(colour = "black"),
+     panel.grid.major = theme_blank(),
+     panel.grid.minor = theme_blank(),
+     panel.border = theme_blank()) +
+     geom_vline(xintercept = 0)

That is more close to what i needed but it does not work and order the bars in the desired order.

Ben Bond-Lamberty

unread,
Dec 13, 2012, 6:17:16 AM12/13/12
to ggp...@googlegroups.com
I would suggest posting a simplified, reproducible example: a toy data
set, and a clear description of exactly what you want to do. Both
Dennis and I are confused, I think.

Ben

Joran

unread,
Dec 13, 2012, 3:36:13 PM12/13/12
to ggp...@googlegroups.com
I explained to you in my comments at StackOverflow how to order the bars along the x axis.
Reply all
Reply to author
Forward
0 new messages