geom_text and legend

431 views
Skip to first unread message

Juliet Hannah

unread,
Aug 28, 2009, 6:16:47 PM8/28/09
to ggp...@googlegroups.com
Hi Group,

This is related to an example posted earlier today for which I
couldn't get the solution. Here
is an example that focuses on this topic.

I used geom_text to label some points by a group. The text is colored
by group, and the
letter 'a' appears in the legend corresponding to the color.

How do I remove this 'a' from the legend?

x <- seq(1:10)
y <- x
mygroups <- c(rep("A",5),rep("B",5))

mydata <- data.frame(x,y,mygroups)
p <- ggplot(data=mydata,aes(x=x,y=y,label=mygroups))
p <- p + geom_point(aes(colour=mygroups))
p <- p + geom_text(hjust=2, aes(colour = mygroups), size = 3)
p


Thanks,

Juliet

baptiste auguie

unread,
Aug 29, 2009, 3:28:05 AM8/29/09
to Juliet Hannah, ggp...@googlegroups.com
Hi,

The only thing that I know of is this hack,

library(ggplot2)
x <- seq(1:10)
y <- x
mygroups <- c(rep("A",5),rep("B",5))

mydata <- data.frame(x,y,mygroups)
p <- ggplot(data=mydata,aes(x=x,y=y,label=mygroups))
p <- p + geom_point(aes(colour=mygroups))
p <- p + geom_text(hjust=2, aes(colour = mygroups), size = 3)
p

GeomText$guide_geom <- function(.) "blank"

p

GeomText$guide_geom <- function(.) "text"

p # back to the initial state

But hopefully there exists a cleaner way to achieve this.

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

Juliet Hannah

unread,
Aug 29, 2009, 9:16:31 AM8/29/09
to baptiste auguie, ggp...@googlegroups.com
Hi Baptiste,

Ok good -- so it wasn't obvious!

I haven't been able to find where this solution is documented. Could
you direct me to it? I can't say I follow what you did. :)

Thanks,

Juliet

baptiste auguie

unread,
Aug 29, 2009, 9:26:05 AM8/29/09
to Juliet Hannah, ggp...@googlegroups.com
Here's where I found it,

http://www.nabble.com/ggplot2:-How-to-remove-legend-component-for-geom_errorbar()---td17861845.html

I guess the reason that this isn't documented is two-fold: i) it's
somewhat against the idea of a "grammar of graphics"; ii) Hadley
mentioned that he might provide a better solution in the future, as
this is clearly not a hack that should be done at the user level.

Best,

baptiste

hadley wickham

unread,
Aug 29, 2009, 9:35:23 AM8/29/09
to baptiste auguie, Juliet Hannah, ggp...@googlegroups.com
> Here's where I found it,
>
> http://www.nabble.com/ggplot2:-How-to-remove-legend-component-for-geom_errorbar()---td17861845.html
>
> I guess the reason that this isn't documented is two-fold: i) it's
> somewhat against the idea of a "grammar of graphics"; ii) Hadley
> mentioned that he might provide a better solution in the future, as
> this is clearly not a hack that should be done at the user level.

There's an experimental undocumented feature that you can also use:

p <- p + geom_text(hjust=2, aes(colour = mygroups), size = 3, legend = F)

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages