Legendary problems in the ggplot scatter graph

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() 
+6
r ggplot2 legend
source share
3 answers

As @Iselzer notes in the comment, two lines are for abline and smooth .

To get the background of the legend with a white fill, you need to fool ggplot as follows:

  • Create a geom_smooth layer with fill rendered in color
  • Create a second, almost identical geom_smooth layer, but this time with a white bay and not mapped to a legend:

The code:

 p=ggplot(df, aes(x=x, y=y)) + geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + geom_smooth(method=lm, fill="red") + 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")) + opts(title="Method Comparison") + labs(x="Control", y="Evaluation") + theme_bw() 

Also note that you can simplify your code with a small bit by using labs() to create labels. This means that you do not need to recreate the scales.

enter image description here

+5
source share

Actually, there is a way to change this without adding a malicious workaround:

 p + theme(legend.key = element_rect(color=NA, fill="white")) 
+2
source share

Here I take the andring usring code without two lines in the legend

 ggplot(df, aes(x=x, y=y)) + geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + geom_smooth(method=lm, fill="red") + geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid" ) + geom_line(data = data.frame(x=0, y=0), 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")) + opts(title="Method Comparison") + labs(x="Control", y="Evaluation") + theme_bw() 

I hope it’s better to avoid this kind of hack in managing the legend in the new version of ggplot.

0
source share

All Articles