ggplot2 - R Order of stacked areas with ggplot geom_area -


i needed reinstall r , encounter little problem ggplot. sure there simple solution , appreciate hints!

i using stacked area plot quite often, , got desired stacking , legend order defining factor levels , plotting in reverse order. however, not working more after re-installation.

here example:

dx <- data.frame(x=rep(1:8,3),y=rep(c(2,3,2,4,3,5,3,2),3),z=c(rep("bread",8),rep("butter",8),rep("fish",8)))  ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z, order=-as.numeric(z))) 

this gives following plot:

enter image description here

it looks if "order" did not have impact on plot.

the desired plot stack areas shown in legend, i.e. red area on top, blue area @ bottom.

where mistake?

many in advance!

you can either use (the colors reversed):

dx$z <- factor(dx$z, levels = rev(levels(dx$z))) ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z)) 

enter image description here

or directly use (without reversing factor levels, won't change color):

ggplot() + geom_area(data=dx, aes(x=x, y=y, fill=z)) +                   guides(fill = guide_legend(reverse=true)) 

enter image description here


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -