I'm a new here but I hope that you can help me. I just googled my problem but coudn't solve it.
I have a data frame containing lots of data which I want to plot with ggplot in R. All does work very well but the legend drives me crazy. The linetypes in the legend are always solid instead of what I defined.
I'm loading a csv file, then making subsets with loops and summarize the subsets with SummarySE().
A subset is looking like this:
ExperimentCombinations LB TargetPosition N C_measured sd se ci
1 HS 0.10 Foveal 10 0.11007970 0.04114193 0.013010221 0.02943116
2 HS 0.21 Foveal 10 0.09821870 0.04838134 0.015299523 0.03460992
3 HS 0.30 Foveal 9 0.07911856 0.04037776 0.013459252 0.03103709
4 HS 1.00 Foveal 11 0.06657355 0.02688821 0.008107099 0.01806374
5 LED 0.10 Foveal 8 0.12569725 0.03607487 0.012754393 0.03015935
6 LED 0.21 Foveal 10 0.08797370 0.02091996 0.006615472 0.01496524
7 LED 0.30 Foveal 10 0.07358290 0.03002596 0.009495042 0.02147928
8 LED 1.00 Foveal 8 0.06630350 0.01894423 0.006697796 0.01583777
in this case TargetPosition has Levels Foveal or Peripheral. The ggplot code I'm using is (looks awful because I was trying to solve my problem...):
ColourFoveal <- c("#FFCC33","#00CCFF")
ColourPeripheral <- c("#FFCC33","#00CCFF")
#ColourPeripheral <- c("#FF9900","#0066FF")
PointType <- c(20,20)
PointTypeSweepUp <- c(24,24)
PointTypeSweepDown <- c(25,25)
ColourHSFillFoveal <- c("#FFCC33", "#FFCC33")
ColourHSFillPeripheral <- c("#FF9900", "#FF9900")
#LineTypeFoveal <- c("solid", "solid")
LineTypePeripheral <- c("dashed","dashed")
xbreaks <- c(0.1,0.21,0.3,1.0)
plotsYmax <- 0.2
if(field=="Peripheral"){
lineType<-sprintf("dashed")
lineColour<-ColourPeripheral
}else{
lineType<-"solid"
lineColour<-ColourFoveal
}
ggplot(df, aes(x=LB, y=C_measured, shape=ExperimentCombinations, colour=ExperimentCombinations)) +
geom_errorbar(aes(ymin=C_measured-se, ymax=C_measured+se), width=.1) +
geom_line(linetype=lineType) +
geom_point() +
ggtitle(paste(targetDataFrame$AgeGroup, targetDataFrame$TargetPosition)) +
scale_colour_manual(name="", values=lineColour)+
scale_shape_manual(name="", values=PointType)+
scale_fill_manual(name="", values=lineColour)+
scale_x_continuous(breaks=xbreaks)+
coord_cartesian(ylim = c(0, plotsYmax+0.01))+
scale_y_continuous(breaks=c(0,0.05,0.1,0.15,0.2))+
theme(axis.line=element_line(colour="black"))+
theme(panel.grid=element_blank())+
theme_bw()+
theme(legend.key.width=unit(2,"line"))
}
The peripheral plots should have dashed lines, the foveal ones solid lines. What I get is always like this: (As a new user I'm not allowed to post images!) The lines are dashed an in the colours I like to, the points are right, too. But in the legend, the lines are solid instead of dashed. The Colours and points are alright in the legend, too.
Could you help me to define the linetypes in the legend as dashed in the peripheral case?