I am currently trying to reorder the stacks in a stacked bargraph with ggplot2 using the order argument. This works like a charm when not using facetting, but upon running facet_grid, only one of the pannels works. It is important to realize that I want to use a custom order, rather than based on size (as suggested in this topic or others concerning the use of order=desc(variable)
).
You should be able to reproduce this issue using the code below
require(dplyr)
require(tidyr)
require(ggplot2)
prod<-1:8
timing<-1:4
variab<-letters[1:3]
nobserv<-length(prod)*length(timing)*length(variab)
values<-runif(nobserv)
plotset<-data.frame(product=rep(prod,each=4,length=nobserv),
timepoints=rep(timing,length=nobserv),
variable=rep(variab,each=nobserv/3),
values=values)
sorter<-rep(c(-2,-1,-3),each=nobserv/3)
p <- ggplot(plotset,aes(fill=variable,order=sorter,x=product,y=as.numeric(as.character(values))))
p <- p + geom_bar(stat='identity', position='stack')
p <- p + facet_grid(~ timepoints)
p
This will generate this graph, clearly showing only timepoint three (3) does what I want.
I do not know why this is and if and how it could be fixed. Thanks in advance for your input!
EDIT: I made a typo, now the issue should be reproducible
ps: I could reproduce the problem on both a linux and a windows machine with R 3.1.1 and ggplot2 1.0.0