1
votes

I have a geom_point plot to which I have added both a geom_smooth regression line (which I want to appear as a dotted grey line) and a geom_abline (which I want to appear as a solid black line). I can do this, but the problem arises when I try to include a legend identifying the two lines.

I have spent over four hours looking at every stackoverflow question that looks vaguely relevant and also the R cookbook section of scale_XXX_manual and legends and I feel like I understand what is required, namely, specify the colour and line type using dummy variables in the aes for the relevant geom and then defining these dummy variables in the scale_XXXX_manual statements using the values= option as I've done in the following code

df <- data.frame(xvar=c(1, 1.7, 2, 2.47, 3.47),
                 yvar=c(-0.543,  0.326,  0.147,  0.554,  1.180),
                 wtvar=c(167,  43, 965, 841,  84))
ggplot(df, aes(x=xvar, y=yvar))+
  geom_point()+
  geom_smooth(method="lm", 
              mapping = aes(weight = wtvar, colour="A", linetype="B"))+
  geom_abline(slope=1, intercept = (-1.3), aes(colour="C", linetype="D"), size=1)+
  ylim(-1,2.5)+
  theme_classic()+
  scale_colour_manual(name="Legend", 
                      labels=c("regression", "benchmark"),
                      values=c(A="grey85", C="black"))+
  scale_linetype_manual(name="Legend", 
                        labels=c("regression", "benchmark"),
                        values=c(B="dashed", D="solid"))

I get the following error:

Error: ggplot2 doesn't know how to deal with data of class uneval

An explanation of why what I've done doesn't work as well as guidance on the correct solution would be very much appreciated.

Update: Aggregating the two aes statements in the geom_smooth as suggested (thanks to Z. Lin - I'd thought mapping option was related just to the regression but I now understand it's all the aesthetics) stops the error message and gets much closer to the desired result. The plot itself looks as it should, but the missing piece in the puzzle is the inclusion of the benchmark line in the legend. I'm getting a legend with just the dashed grey line identified as the regression line.

1
You included two aes(...) mappings in geom_smooth(). That's probably what triggered the error message. (I got a similar but not identical error message from that line... which could be due to different package versions having different messages.)Z.Lin

1 Answers

0
votes

Try putting the slope and intercept within geom_abline aes

ggplot(df, aes(x=xvar, y=yvar))+
  geom_point()+
  geom_smooth(method="lm", 
              aes(weight = wtvar, colour="A", linetype="B"))+
  geom_abline(aes(slope=1, intercept = (-1.3), colour="C", linetype="D"), size=1)+
  ylim(-1,2.5)+
  theme_classic()+
  scale_colour_manual(name="Legend", 
                      labels=c("regression", "benchmark"),
                      values=c(A="grey85", C="black"))+
  scale_linetype_manual(name="Legend", 
                        labels=c("regression", "benchmark"),
                        values=c(B="dashed", D="solid"))