Unwanted extra legend

69 views
Skip to first unread message

MK

unread,
Feb 11, 2012, 10:49:03 AM2/11/12
to ggplot2
Please run the following code for the first figure, and then compare
it with the second, which has an extra superfluous legend. Please tell
me (1) how to fix this, and (2) where I would find the documentation
that would have clarified this issue?


library( ggplot2 )
library( effects )
effectdf <- function( ... ){
suppressWarnings( as.data.frame( effect( ... )))
}
data( ToothGrowth )
tg <- ToothGrowth
tg$dose.f <- factor( tg$dose )
m1 <- lm( len ~ supp * dose.f, data = tg )
m1.ef <- effectdf( 'supp:dose.f', m1 )
m1.ef$dose <- m1.ef$dose.f
m1.ef$dose.f <- factor(m1.ef$dose.f)
#############
#1st figure: dose is factor
#############
pd <- position_dodge( 0.1 )
p <- ggplot( m1.ef, aes( x = dose.f, y = fit,
colour = supp ) )
p <- p + geom_point( position = pd )
p <- p + geom_line( aes( group = supp ), position = pd )
p <- p + xlab( 'dose' ) + ylab( 'mean(length)' )
p <- p + geom_errorbar(
aes( ymin = lower, ymax = upper, width = 0.1 ),
position = pd )
( p <- p + opts( legend.position = c( 0.66, 0.33 ) ) +
scale_colour_discrete( name = 'supplement',
breaks = c( 'OJ', 'VC' ),
label = c( 'orange juice', 'ascorbic acid' ) ) )
#############
# 2nd figure: dose is continuous (but using points from 1st analysis)
#############
m2 <- lm( len ~ supp * dose, data = tg )
m2.ef <- effectdf( 'supp:dose', m2 )
pd <- position_dodge(0.05)
p <- ggplot( m2.ef, aes( x = dose, y = fit, colour = supp ) )
p <- p + xlab( 'dose' ) + ylab( 'mean(length)' )
p <- p + geom_point( data = m1.ef, aes( y = fit ),
position = pd )
p <- p + geom_smooth(
aes( y = fit, ymin = lower, ymax = upper, fill = supp ),
stat = 'identity', alpha = 0.2)
( p <- p + opts( legend.position = c( 0.66, 0.33 ) ) +
scale_colour_discrete( name = 'supplement',
breaks = c( 'OJ', 'VC' ),
label = c( 'orange juice', 'ascorbic acid' ) ) )

Ista Zahn

unread,
Feb 11, 2012, 11:09:18 AM2/11/12
to ggp...@googlegroups.com
Hi,

You can change the settings of scale_fill to match those of scale_color, which
will cause the two legends to be collapsed into one. Just add1

scale_fill_discrete( name = 'supplement',


breaks = c( 'OJ', 'VC' ),
label = c( 'orange juice', 'ascorbic acid' ))

to your final p

Best,
Ista

label = c( 'orange juice', 'ascorbic acid' ) ) +
scale_fill_discrete( name = 'supplement',

Michael Kubovy

unread,
Feb 11, 2012, 12:48:09 PM2/11/12
to Ista Zahn, ggp...@googlegroups.com
Thanks so much. That works.

How could I have found out that the source of the second legend was geom_smooth()?

MK

> --
> 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

Dennis Murphy

unread,
Feb 11, 2012, 1:24:37 PM2/11/12
to Michael Kubovy, Ista Zahn, ggp...@googlegroups.com
Hi:

geom_smooth() was the source only because you mapped the fill
aesthetic to supp inside its aes(). By default, every mapped,
non-positional aesthetic (e.g, fill, color, size) generates its own
legend. A variable is mapped to an aesthetic inside an aes() call;
aesthetics can be set to constant values outside of aes(); e.g., size
= 3.

To merge two legends, they need to have the same legend title and the
same breaks, values and labels; that's usually done with
scale_aesthetic_manual(), as Ista pointed out (where 'aesthetic' is a
placeholder for the actual aesthetic).

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages