I\'m creating a plot with multiple linetypes, colors, and filled regions.The code below produces two legends (one shows line types, the other shows line colors) - and I need
As said in the comment by @joran You need to create a variable state and bind it to color
and linetype
then ggplot do the rest for you. For example ,Here the plot s very simple since you put data in the right shape.
To manipulate data you need I advise you to learn plyr
and reshape2
!
## I use your data
## I melt my data
library(reshape2)
measure.vars <- colnames(dat)[c(1:12)][-c(1,3,5,10)]
mydata.melted <- melt(mydata,
measure.vars= measure.vars, ## I don't use all the variables only
States ones
id.vars='Dates')
states.list <- list('a','b','c','d','e','f','g','h')
names(states.list) <- measure.vars
mydata.melted$State <- NA
library(plyr)
mydata.melted <- ddply(mydata.melted,
.(variable),transform,
State=states.list[[unique(variable)]])
## I plot using the rights aes
stochdefcolor = 'red'
stochoptcolor = 'green'
dtrmndefcolor = 'darkred'
dtrmnoptcolor = 'darkgreen'
library(ggplot2)
ggplot(subset(mydata.melted)) +
geom_line(aes(x=Dates,y=value,
color=State,linetype=State))+
scale_linetype_manual(values=c(1,1,1,1,2,3,2,3)) +
scale_size_manual(values =rep(c(1,0.7),each=4))+
scale_color_manual(values=c(stochdefcolor,stochoptcolor,
dtrmndefcolor, dtrmnoptcolor,
stochdefcolor,stochdefcolor,
stochoptcolor,stochoptcolor))