Reputation: 10506
I have this situation where I am plotting a series of lines, something like the following:
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))
Which produces a plot as follows:
If you look closely at the origin, the values of high index value (light blue) are stacked on top of the lines with low index value (dark blue).
How can I reverse this, so that the dark blue lines (low index) are stacked on top of the light blue lines (high index).
The above is analogous to my actual problem, where I am plotting scientific data for various temperatures. The low temperature values are more meaningful than the high temperature values, so I do not want the low temperature lines to be potentially masked by the high temperature lines, in fact the opposite is my preference.
Upvotes: 6
Views: 358
Reputation: 77096
I believe reversing the group order should do it (without changing the colours),
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))
Upvotes: 2
Reputation: 93761
I think all you need to do is change the colour aesthetic to rev(IX)
, which will reverse the ordering, and you don't need group
or order
:
ggplot(df.test, aes(x=theta, y=value, color=rev(IX))) + ...
Upvotes: 2