Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2, line stacking order for aesthetic mapping of variable

Tags:

r

ggplot2

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:

Sample

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.

like image 271
Nicholas Hamilton Avatar asked Jul 25 '15 02:07

Nicholas Hamilton


People also ask

What is aesthetic mapping in ggplot2?

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.

What is the group aesthetic in a plot?

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

What happens when ggplot2 changes the Order of rows?

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.

What is default mapping set in ggplot?

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 ().


1 Answers

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))

enter image description here

like image 123
baptiste Avatar answered Nov 15 '22 10:11

baptiste