ggplot2,行堆叠顺序,用于变量的美学映射

Nic*_*ton 6 r ggplot2

我有这种情况,我正在绘制一系列行,如下所示:

library(foreach)
library(ggplot2)

df.test <- foreach(ix = seq(0,1,by=0.01),.combine=rbind) %do% {
  theta = seq(-pi,pi,by=pi/100)
  data.frame(IX=ix,theta=theta,value=ix*sin(theta))
}
ggplot(df.test,aes(x=theta,y=value,color=IX,group=IX,order=-IX)) + geom_path() +
  theme_bw() + labs(x="Theta",y="Sin(Theta)",color="Index",title="Example Plot") +
  theme(legend.position=c(0,1),legend.justification=c(0,1))
Run Code Online (Sandbox Code Playgroud)

其中产生如下情节:

样品

如果仔细观察原点,高指数值(浅蓝色)的值会堆叠在具有低索引值(深蓝色)的行的顶部.

如何将其反转,使深蓝色线条(低指数)堆叠在浅蓝色线条(高指数)之上.

以上类似于我的实际问题,我正在绘制各种温度的科学数据.低温值比高温值更有意义,因此我不希望低温线可能被高温线屏蔽,实际上我的偏好相反.

bap*_*ste 2

我相信颠倒组顺序应该可以做到(不改变颜色),

ggplot(df.test,aes(x=theta,y=value,color=IX,group=rev(IX))) + 
    geom_path(lwd=2) +
    theme_bw() + 
    labs(x="Theta",y="Sin(Theta)",color="Index",title="Example Plot") +
    theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
    coord_cartesian(xlim=c(-0.2,0.2), ylim=c(-0.2,0.2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述