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.
Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms. Aesthetic mappings can be set in ggplot2() and in individual layers. aes(x, y, ...) List of name value pairs giving aesthetics to map to variables.
The group aesthetic is by default set to the interaction of all discrete variables in the plot. […] For most applications the grouping is set implicitly by mapping one or more discrete variables to
This breaks containment, so that the plot no longer contains everything it needs, and causes problems if ggplot2 changes the order of the rows, as it does when faceting. Aesthetic mappings can be supplied in the initial ggplot () call, in individual layers, or in some combination of both.
If NULL, uses the default mapping set in ggplot (). data: A dataset which overrides the default plot dataset. It is usually omitted (set to NULL ), in which case the layer will use the default data specified in ggplot ().
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With