0

I've produced a graph from my data in RStudio using ggplot2, and to show the comparison between my x/y variables, I've added the line y=x using geom_abline():

ggplot(data=my_df, aes(x=x, y=y)) +
    geom_point() +
    geom_abline(aes(slope=1, intercept=0, colour="x=y")) +

(see image of graph here)

The code is generalised and omits lines for my axis labels etc. The code within geom_abline() is my closest attempt at generating the legend. You can see that the legend title is "colour", but I want to change it to "legend" and haven't found a way to do so.

This is different from the stack exchange qu. "How to change legend title in ggplot" because I'm modifying the legend of a new line added, not my original plots, thus the answers in that question have had no effect. I don't understand what parameters like colour = "x=y" are doing within abline or how to change them.

Version of RStudio: 1.0.143

Version of ggplot2: 2.2.1

ddm_ingram
  • 91
  • 9
  • The solutions in the question you liked to did not solve my case, so I have edited my question to make this clear. – ddm_ingram May 05 '17 at 10:01
  • Not sure if this will be re-opened for you, but it sounds like you may want to create a new mapping specifically for the lines you want to draw . For example: slope_df <- data.frame(a = c(0,0,1), b= c(0,1,1), line = c("y = 0", "y = x","y = 1 + x")) then: ggplot(data=my_df, aes(x=x, y=y)) + geom_point() + geom_abline(data=slope_df,aes(intercept=a, slope=b,colour = line)) – johnckane May 05 '17 at 20:00

0 Answers0