Adding a legend to ggplot

I'm endeavoring to add a legend after creating a ggplot showing regression lines. I used the following code, which creates the plot, but doesn't seem to give me any option to add a legend. (It appears geom_abline automatically sets show.legend to FALSE if you specify a particular slope or intercept.) Can someone advise the best way to do this? Appreciate your advice.

ggplot(dat, aes(y=PCTGRAD,x=SAT))+geom_point(cex=0.5)+geom_abline(intercept=private.intercept,slope=slope,col="red", show.legend=TRUE)+geom_abline(intercept=public.intercept, slope=slope, col="blue", show.legend=TRUE)

Legends are usually added by setting an aesthetic in the mapping with the aes() function.

ggplot(DF, mapping = aes(x = Date, y = Value, color = GRP)) + ...

That would make a legend mapping the colors to the values of the GRP column.
Can you explain more about your data and what you are trying to do? A toy example of your data would also be very helpful. You can post data here using the output of the dput() function or you can constrict a small data set using the data.frame() function.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.