示例数据框:
df <- data.frame(x=rep(1:10,4),y=rnorm(40),Case=rep(c("B","L","BC","R"),each=10))
我可以使用以下方法在其自己的方面绘制每个时间序列:
ggplot(df,aes(x=x,y=y,color=Case)) + geom_line()+facet_wrap(~Case,nr=2,nc=2)
Now, suppose I want to change the facet order to (starting from top-left and going to bottom-right along rows) "L","B","R","BC". The usual suggestion here on SO is to do this https://stackoverflow.com/questions/18066870/reorder-levels-of-facet-wrap-using-ggplot. However, if I reorder the levels of factor Case
, also the colors of the curves will be changed. Is it possible to reorder the facets, without changing also the order of the curve colors? I know it sounds like a weird question. The problem is that my report is a work in progress, and in a older version of the report, which I already showed to coworkers & management, there were multiple plots more or less like this (with no facet_wrap
):
ggplot(df,aes(x=x,y=y,color=Case)) + geom_line()
换句话说,现在人们已经习惯将“B”与红色联系起来。如果我重新排序,“L”将是红色,“B”将是绿色,依此类推。我已经听到了抱怨……有什么办法可以解决这个问题吗?