user278411 - 9 months ago 50

R Question

I have a dataset where some variables always have negative values and some always have positive values. I want to make a stacked area plot where the negative values are stacked below the x axis and the positive values above. I have the variable names as factors in a specific order in my data frame which I want to appear in the legend. The problem is that, in order to plot data both above and below the x axis, I have to use geom_area() twice, once with a positive subset of my data, and once with the negative subset. When I do this, it realphabetizes the factors in the legend, disregarding their original order.

`include('ggplot2')`

test <- data.frame(x=rep(c(1,2,3,4),4),

fact=factor(c(rep('a',4),rep('b',4),rep('c',4),rep('d',4)),

levels=c('c','a','d','b'), ordered=TRUE),

y=c(4,3,2,1,-4,-3,-2,-1,8,6,4,2,-8,-6,-4,-2))

# The legend displays the factors in the order I want (c,a,b,d)

ggplot(data=test, mapping=aes(x=x, y=abs(y)))+

geom_area(mapping=aes(fill=fact))

# This doesn't work for plotting above and below the x axis,

# so I have to split it into two different geom_area calls.

ggplot(data=test, mapping=aes(x=x, y=y))+

geom_area(mapping=aes(fill=fact))

# Factors in legend are now reordered alphabetically,

# but I want them to be ordered c,a,b,d.

ggplot(data=test, mapping=aes(x=x, y=y))+

geom_area(data=test[test$y<0,], mapping=aes(fill=fact), stat='identity')+

geom_area(data=test[test$y>0,], mapping=aes(fill=fact), stat='identity')

How can I get the factors in the legend to appear as I want?

Source (Stackoverflow)