Mix geom_point and geom_abline, legend problem

1,371 views
Skip to first unread message

thomas

unread,
Dec 3, 2010, 7:38:20 PM12/3/10
to ggplot2
Hi,

I have a geom_point plot with two groups and shapes. i also add 2
horizontal lines via geom_abline. my problem is the legend. it seems
to generate two legends. one for the shapes, and one for the 4 colours
(two for the two groups from geom_point and one for each abline)

code:

p <- ggplot(data,aes(idx,c,colour=type,group=type, shape=type)) +
geom_point()
p <- p + geom_abline(aes(intercept=23,slope = 0,colour =
"#000000"),legend=F,linetype=4) + geom_abline(aes(intercept=8,slope =
0, colour="7A7A7A"),linetype=2,legend=F)

output:
http://yfrog.com/9fggplotj

Now i am wondering how one would solve that? Can i 'merge' those two
legends, despite the fact that abline has no 'shape'.

help is appreciated, as usual ;)

thanks in advance,
thomas

Brandon Hurr

unread,
Dec 3, 2010, 9:34:30 PM12/3/10
to thomas, ggplot2
I really don't understand how you have two shapes and 4 colors when both of them are "=type". 

If you leave off the abline calls in your plot what does the plot look like? 

Can you supply some sample data? 

Do you actually need to use abline? What about hline?

B


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

thomas

unread,
Dec 4, 2010, 6:49:05 AM12/4/10
to ggplot2
hi,

data:
c, idx,type,corpus
5.64643799472296,0,A,100k
6.80738786279683,1,A,100k
1.74142480211082,2,A,100k
54.1424802110818,3,A,100k
6.38522427440633,4,B,100k
10.3430079155673,5,B,100k

> I really don't understand how you have two shapes and 4 colors when both of
> them are "=type".

i don't understand why i wouldn't have 2 shapes and 4 colors.

> If you leave off the abline calls in your plot what does the plot look
> like?

http://yfrog.com/bfwithoutlj

> Do you actually need to use abline? What about hline?http://had.co.nz/ggplot2/geom_hline.html

Not anymore. I change it to hline but that doesn't change anything for
me.

T

Dennis Murphy

unread,
Dec 4, 2010, 6:59:21 PM12/4/10
to thomas, ggplot2
Hi:

The problem arises because you have mapped color for the points and then again for the lines. If you don't want the line colors in the legend, put them outside the aes() statement in geom_abline()- that will set single colors to individual lines, which appears is what you want. By putting colour inside aes() for the geom_abline() calls, you're adding them to the list of colors in the legend you defined in the ggplot() call. That makes the legends for color and shape different, which is why they appear separately in the plot. I believe that if you *set* the color inside the geom_abline() calls, all will be well :)

(i.e., geom_abline(aes(intercept = 23, slope = 0), colour = '#000000', linetype = 4) + ...

or equivalently,
geom_hline(yintercept = 23, colour = '#000000', linetype = 4) + ...

HTH,
Dennis

thomas

unread,
Dec 4, 2010, 7:48:39 PM12/4/10
to ggplot2
Hi Dennis,

> If you don't want the line colors in the legend

Oh no, I want all four colours being present in the legend.
There is no need for a second legend since the shape comes from
geom_point(), which is already represented in the legend (through the
colors). Hence merging both legends into one is the obvious goal.

And as you have said, putting colour outside of aes() does indeed
clean up the legend, but now i don't see the hline-color in the
legend...

Best,
Thomas

Dennis Murphy

unread,
Dec 5, 2010, 12:15:17 AM12/5/10
to thomas, ggplot2
Hi:

On Sat, Dec 4, 2010 at 4:48 PM, thomas <thk....@gmail.com> wrote:
Hi Dennis,

> If you don't want the line colors in the legend

Oh no, I want all four colours being present in the legend.
There is no need for a second legend since the shape comes from
geom_point(), which is already represented in the legend (through the
colors). Hence merging both legends into one is the obvious goal.

The problem you have is four colors and two shapes. The mismatch in number between them is enough to produce separate legends for color and shape since it's impossible to set a shape to a line (linetype handles that). You might try some trickery with scale_colour_manual and scale_shape_manual, but that may be the only way. To merge two scales, you need the same title and the same set of breaks, if I recall correctly. As it stands now, you have four breaks for colour and two for shape.

Dennis

And as you have said, putting colour outside of aes() does indeed
clean up the legend, but now i don't see the hline-color in the
legend...

Best,
Thomas

--

Thomas Kern

unread,
Dec 5, 2010, 9:04:46 AM12/5/10
to Dennis Murphy, ggplot2
Hi,

> try some trickery with scale_colour_manual and scale_shape_manual, but that
> may be the only way. To merge two scales, you need the same title and the
> same set of breaks, if I recall correctly. As it stands now, you have four
> breaks for colour and two for shape.

I just tried scale_*_manual and it can indeed merge the two legends.
The only problem is that when you set
scale_shape_manual(values=c(1,2,3,4)), the two hline-legend-entries
actually have the same shape, although the lines don't.
looks like this: http://yfrog.com/julegendszj

Not sure how to change that though.

Thomas

Reply all
Reply to author
Forward
0 new messages