Dear ggplotters,
I'm new to R and ggplot, and I'm having some trouble running code that has previously worked, but not today.
The code below replicates my issue. When creating a stacked bar graph previously, the first factor would be put at the bottom of the stack. To create labels that were positioned correctly, you could arrange appropriately, and calculate a cumulative sum for the label position.
However, it now appears that the first factor is on top of the stack, which messes with the label positioning. I've run the examples from the R Graphics Cookbook and I'm noticing the same thing. I've uninstalled and re-installed ggplot to with no change.
Is this a new feature, so that the stack order matches the legend order? Do I need to change all my scripts to re-calculate the label positions based on the reverse order?
library(plyr)
library(ggplot)
df <- data.frame(x = c("a", "b"), fill = c("x","x","y","y"), y = c(1,2,3,4))
arr <- arrange(df, x, fill)
arr <- ddply(arr, "x", transform, label_y = cumsum(y))
ggplot(arr, aes(x = x, y = y, fill = fill)) +
geom_bar(stat = "identity")+
geom_text(aes(y=label_y, label=y), vjust = 1.5, colour = "white")
Thanks for any feedback!