[R] ggplot2: mixing colour and linetype in geom_line

Matthieu Dubois matthdub at gmail.com
Thu Sep 10 21:36:18 CEST 2009


Hi Benoit, 

I'm not a specialist of ggplot2, but I will try to help. 
You may obtain more --interesting-- answers on the ggplot2 
mailing list. This said, let's go. 

To solve your problem, I would suggest to 
1. change the form of the data frame (using the reshape library) 
in order to have one variable for Temp, one for the different Xs, 
and one for their y values. 
2. add a new variable for the different molecules.
3. then plot


# change the format of the data frame
library(reshape)
mdat <- melt(THT_N2_ATGMS, id="Temp")

# add the molecule variable 
mdat$mol <- 'other'
mdat$mol[mdat$variable %in% c('X22','X44')] <- 'CO2'
mdat$mol[mdat$variable %in% c('X43','X45')] <- 'AA'

#plot
library(ggplot2)
p <- ggplot(data=mdat, aes(x=Temp, y=value, colour=mol, 
                linetype=variable)) + 
geom_line()
p

that's it. HTH

Matthieu




More information about the R-help mailing list