Following the answer to my former question I have another question raising :
How, without reshaping the data, to plot a stacked bar plot with different colour depending on another category, at the same time using stats="identity" to sum up values for each stacked area ?
The stats identity works nicely to sum up the values, but for non-stacked columns. In a stacked column, the stacking is somehow "multiplied" or "striped", see picture below.
Some data sample :
element <- rep("apples", 15)
qty <- c(2, 1, 4, 3, 6, 2, 1, 4, 3, 6, 2, 1, 4, 3, 6)
category1 <- c("Red", "Green", "Red", "Green", "Yellow")
category2 <- c("small","big","big","small","small")
d <- data.frame(element=element, qty=qty, category1=category1, category2=category2)
Which gives that table :
id element qty category1 category2
1 apples 2 Red small
2 apples 1 Green big
3 apples 4 Red big
4 apples 3 Green small
5 apples 6 Yellow small
6 apples 2 Red small
7 apples 1 Green big
8 apples 4 Red big
9 apples 3 Green small
10 apples 6 Yellow small
11 apples 2 Red small
12 apples 1 Green big
13 apples 4 Red big
14 apples 3 Green small
15 apples 6 Yellow small
Then :
ggplot(d, aes(x=category1, y=qty, fill=category2)) + geom_bar(stat="identity")
But the graph is a bit messy: the colors aren't grouped together !
Why is there this behaviour?
Is there still an option to correctly group the colors without reshaping my data ?
stat = identity
will just draw what you give it. In your case, a messy dataset. You'll have to manually process the table to give you the desired result (which I don't understand what it should look like). – Roman Luštrik