Create legend for geom_abline

2,222 views
Skip to first unread message

Brian Weaver

unread,
Mar 25, 2015, 11:59:12 AM3/25/15
to ggplot2
Hello,

consider the following example:

dat <- data.frame(x = c(1:5,1:5), y = c(1:5,c(2,4,6,8,10)))
g.lines <- data.frame(slope = c(1,2),intercept= rep(0,2),group = factor(1:2))

ggplot() + geom_point(data=dat,aes(x=x,y=y)) +
  geom_abline(data=g.lines,aes(slope=slope,intercept=intercept,linetype=group))

I would like the plot to create a legend for linetype within geom_abline.  But I cannot get it to work.  I have tried scale_linetype_manual, but this does not work either.

I thank you all in advance.

Brian

Ben Bond-Lamberty

unread,
Mar 25, 2015, 1:08:12 PM3/25/15
to Brian Weaver, ggplot2
You need to add `show_guide=TRUE` to the geom_abline() call; by
default it's FALSE (see the help page).

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Weaver

unread,
Mar 25, 2015, 1:24:39 PM3/25/15
to Ben Bond-Lamberty, ggplot2
Wow such a simple solution. Thank you very much for your help.  I did browse the help page but I admit I  did not read it carefully enough :).

Brian
Reply all
Reply to author
Forward
0 new messages