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