Using geom_line and geom_point with only one legend.

2,475 views
Skip to first unread message

Manuel Holtgrewe

unread,
Feb 19, 2010, 7:22:13 AM2/19/10
to ggplot2
Hi,

is it possible to create a plot with both lines and points and have
only one legend?

Currently, I use the following to create my plot:

p <- ggplot(molten.data, aes(x=max_errors, y=value, colour=mapper,
group=mapper, shape=mapper), summary="asdf")
p <- p + facet_wrap(~ variable, scales="free_y", ncol=1)
p <- p + geom_line()
p <- p + geom_point()
p <- p + scale_x_discrete(name='max error')
p <- p + scale_y_continuous('')
p <- p + scale_colour_discrete('read mapper')
p <- p + theme_bw()

The plot looks allright, there are markers at my data points and
lines connecting them. However, there is a key for both data points
with the right shape (all black) and one for the lines with the right
color (but the wrong shape). Is there a way to "merge" the legend?

hadley wickham

unread,
Feb 19, 2010, 8:45:12 AM2/19/10
to Manuel Holtgrewe, ggplot2
Hi Manuel,

Can you please provide a reproducible example by including some of
your data with dput?

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Brian Diggs

unread,
Feb 19, 2010, 12:56:58 PM2/19/10
to ggplot2

On Feb 19, 4:22 am, Manuel Holtgrewe <zyklenf...@googlemail.com>
wrote:


> Hi,
>
> is it possible to create a plot with both lines and points and have
> only one legend?

Legend merging happens automatically when they have the same title and
level labels (it may be more complicated than that, but this is the
general idea; see Section 6.5, p111 of the ggplot2 book, if you have a
copy of that).

> Currently, I use the following to create my plot:
>
> p <- ggplot(molten.data, aes(x=max_errors, y=value, colour=mapper,
> group=mapper, shape=mapper), summary="asdf")
> p <- p + facet_wrap(~ variable, scales="free_y", ncol=1)
> p <- p + geom_line()
> p <- p + geom_point()
> p <- p + scale_x_discrete(name='max error')
> p <- p + scale_y_continuous('')
> p <- p + scale_colour_discrete('read mapper')

You changed the name of the colour scale, but not the shape scale.
Change the shape scale as well and they should merge. (Untested, since
you don't have a self-contained example).

p <- p + scale_shape_discrete("read mapper")

> p <- p + theme_bw()
>
> The plot looks allright, there are markers at  my data points and
> lines connecting them. However, there is a key for both data points
> with the right shape (all black) and one for the lines with the right
> color (but the wrong shape). Is there a way to "merge" the legend?

--Brian Diggs

Manuel Holtgrewe

unread,
Feb 21, 2010, 7:02:09 AM2/21/10
to ggplot2
Thanks, that worked fine.
Reply all
Reply to author
Forward
0 new messages