user278411 user278411 - 7 months ago 44
R Question

Specifying order of factors in legend when plotted data comes from two separate data frames.

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.


test <- data.frame(x=rep(c(1,2,3,4),4),
levels=c('c','a','d','b'), ordered=TRUE),

# The legend displays the factors in the order I want (c,a,b,d)
ggplot(data=test, mapping=aes(x=x, y=abs(y)))+

# 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))+

# 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?


You can manually specify the breaks:

+ scale_fill_hue(breaks = c('c','a','b','d'))