Is it possible to reorder only the faces of facet_wrap without changing the basic levels of factors?

Example data frame:

df <- data.frame(x=rep(1:10,4),y=rnorm(40),Case=rep(c("B","L","BC","R"),each=10)) 

I can build each time series on my face with:

 ggplot(df,aes(x=x,y=y,color=Case)) + geom_line()+facet_wrap(~Case,nr=2,nc=2) 

enter image description here Now suppose that I want to change the order of the facet (starting from the upper left corner and ending at the bottom right in the rows) "L", "B", "R", "BC". The usual suggestion here about SO is to do this . However, if I reorder the levels of the Case factor, the colors of the curves will also change. Is it possible to reorder faces without also changing the order of the colors of the curve? I know this sounds like a weird question. The problem is that my report is incomplete, and in an earlier version of the report, which I already showed to employees and management, there were several or more similar sections (without facet_wrap ):

 ggplot(df,aes(x=x,y=y,color=Case)) + geom_line() 

enter image description here

In other words, by now people are used to associating “B” with red. If I reorder, “L” will be red, “B” will be green, etc. I already hear complaints ... anyway from this?

+8
r ggplot2 facet-wrap
source share
1 answer

Just copy the data into a new column with the order you need. Use the new column for the cut, the old column for the color.

 df$facet = factor(df$Case, levels = c("L", "B", "R", "BC")) ggplot(df, aes(x = x, y = y, color = Case)) + geom_line() + facet_wrap(~facet, nr = 2) 
+4
source share

All Articles