r - Is it possible to reorder only the facets of facet_wrap, without reordering the underlying factor levels? -


sample data frame:

df <- data.frame(x=rep(1:10,4),y=rnorm(40),case=rep(c("b","l","bc","r"),each=10)) 

i can plot each time series in own facet 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 want change facet order (starting top-left , going bottom-right along rows) "l","b","r","bc". usual suggestion here on this. however, if reorder levels of factor case, colors of curves changed. possible reorder facets, without changing order of curve colors? know sounds weird question. problem report work in progress, , in older version of report, showed coworkers & management, there multiple plots more or less (with no facet_wrap):

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

enter image description here

in other words, people used associate "b" red color. if reorder, "l" red, "b" green, , on. can hear complaints...any way out of one?

just copy data new column order want. use new column faceting, old column 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) 

Comments