I want to use ggplot to create scatter charts showing method comparison data. Charts should have raw data, a perfect line and an inline error line. The legend should display the line type / line width / line color for ideal and set lines.
I can get most of what I want, but have these problems with the legend:
the legend shows 2 lines for each type of line, why ?, how to fix?
I would prefer a non-pink background in the legend rectangles (if I do not specify a fill color, the back rectangle will turn gray by default, which I don't like better)
Code example:
set.seed(603) x.raw=rnorm(n=30, mean=50, sd=20) y.raw=x.raw+rnorm(n=30, mean=2, sd=2) x.raw=round(x.raw, 2); y.raw=round(y.raw, 2) df=data.frame(x=x.raw, y=y.raw) require(ggplot2, quietly=TRUE) theme_set(theme_bw()) xy.range=range(df$x, df$y) p=ggplot(df, aes(x=x, y=y)) + geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + geom_smooth(method=lm, fill="red1", aes(colour="Fitted", linetype="Fitted")) + geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) + scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + scale_x_continuous(name="Control", limits=xy.range) + scale_y_continuous(name="Evaluation", limits=xy.range) + opts(title="Method Comparison") p
I really appreciate that you spend all your time answering. Although there is a logic to what works, I would not get trial and error there. I changed the code a bit for final:
- made geom_point last so that points are not overwritten.
- continuous calls are supported, so the x and y axis constraints must be the same
- A similar note added by aspect.ratio = 1, now the perfect line goes from corner to corner at 45 Β° al la Cleveland.
final code:
ggplot(df, aes(x=x, y=y)) + geom_smooth(method=lm, se=FALSE, size=1, aes(colour="Fitted", linetype="Fitted")) + geom_smooth(method=lm, fill="red", colour="red", linetype="twodash", size=1) + geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal"), size=1) + #geom_abline(intercept=0, slope=1, aes(colour = "Ideal", linetype = "Ideal"), size=0) + geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid", size=1) + geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + scale_x_continuous(name="Control", limits=xy.range) + scale_y_continuous(name="Evaluation", limits=xy.range) + opts(title="Method Comparison", aspect.ratio=1) + theme_bw()