r - ggplot2 - Graph with line and dots for two data sets legend issues -


i'm graphing 2 data sets ggplot. 1 should line, other should points. can working below:

d1 <- filter(d, variable==linevar) d2 <- filter(d, variable==dotvar) g <- ggplot(d1, aes(clarity, x=xv, y=yv)) g <- g + geom_line(aes(colour=variable)) g <- g + ggtitle(title) g <- g + xlab(xl) g <- g + ylab(yl) g <- g + geom_point(data=d2, size=4, aes(colour=variable)) 

output of ggplot2

the issue legend! can see, "observed" data set has line + point in legend, when should point. , reverse "predicted", should line.

is there way cleaner / more accurate legend?

you can change legend without changing plot using override.aes. didn't provide sample data, i've used built-in mtcars data frame illustration. key line of code begins guides. shape=c(16,na) gets rid of 1 of legend's point markers setting colour na. linetype=c(0,1) gets rid of other legend's line setting linetype 0. also, don't need save plot after each line of code. add + each line , string them in single statement, in example below.

library(reshape2) library(ggplot2)  mtcars$mpg.line = mtcars$mpg mtcars.m = melt(mtcars[,c("mpg","mpg.line","wt")], id.var="wt") mtcars.m$variable = factor(mtcars.m$variable)  ggplot() +   geom_line(data=mtcars.m[mtcars.m$variable=="mpg.line",],              aes(wt, value, colour=variable), lwd=1) +   geom_point(data=mtcars.m[mtcars.m$variable=="mpg",],              aes(wt, value, colour=variable), size=3) +   guides(colour=guide_legend(override.aes=list(shape=c(16,na), linetype=c(0,1)))) +   theme_grey(base_size=15) 

enter image description here


Comments