removing legends of geom_point() and geom_text()

4,344 views
Skip to first unread message

galneweinhaw

unread,
Apr 1, 2011, 10:15:37 PM4/1/11
to ggplot2
I have a plot that ends up having at least 3 legends, 2 overlapping
and one I don't knwo where it comes from:

library(ggplot2)
polls <- read.csv("Polls2011.csv", header=TRUE)
election <- read.csv("Election2008.csv", header=TRUE)
windows(width = 8, height = 5)
ggplot(polls, aes(x = Date, y = Popular_Support, colour=Party)) +
geom_point() +
#stat_smooth(span = 1.0) +
scale_colour_manual(values = c(Conservative=alpha("blue",
1),Liberal=alpha("red",1),NDP=alpha("orange",1), Green=alpha("green3",
1),Bloc=alpha("turquoise4", 1))) +
geom_point(data=election, size=3, shape=5) +
geom_point(data=election) +
geom_text(data=election, aes(label = c(37.6,26.2,18.2,"10.0",6.8),
size=3, hjust=-0.3, vjust=-0.2))

How can I remove some of the legends?

kahsay gebremariam

unread,
Apr 2, 2011, 2:57:15 PM4/2/11
to galneweinhaw, ggplot2
did u try 
opts(legend.position = "none") 





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

Tylere Couture

unread,
Apr 2, 2011, 5:35:24 PM4/2/11
to kahsay gebremariam, ggplot2
Thanks, unfortunately that removes all the legends.

This solution got part way there: +geom_point(...legend=F...) +
http://groups.google.com/group/ggplot2/msg/f49d03584efe2881

Bu there is still this second legend that is sticking around from geom_text...

Tylere Couture

unread,
Apr 2, 2011, 5:38:22 PM4/2/11
to kahsay gebremariam, ggplot2
The extra legend is the one with the 3's in it...
2011FederalElectionPolls.png

Dennis Murphy

unread,
Apr 2, 2011, 6:04:55 PM4/2/11
to Tylere Couture, ggplot2
Hi:

Do this instead:

geom_text(data=election, aes(label = c(37.6,26.2,18.2,"10.0",6.8)),
                      size=3, hjust=-0.3, vjust=-0.2)

Put size outside of aes(). You probably need to do some reading to learn the distinction between setting and mapping aesthetics. For example, size = 3 sets the aesthetic size to the (single) value 3, which is properly done outside an aes() call. Inside aes(), variables are mapped to aesthetics. In your plot, you mapped color to Party => different values of Party are assigned different colors. Legends are created for variables that are mapped to certain aesthetics (e.g., color, fill, shape, size), but not for variables that are set to constant values.

HTH,
Dennis

On Sat, Apr 2, 2011 at 2:35 PM, Tylere Couture <tylere...@gmail.com> wrote:

Tylere Couture

unread,
Apr 2, 2011, 6:26:32 PM4/2/11
to Dennis Murphy, ggplot2
Thank you!
Reply all
Reply to author
Forward
0 new messages