Problem with geom_text

10 views
Skip to first unread message

Raivo Kolde

unread,
Aug 19, 2009, 8:02:59 AM8/19/09
to ggp...@googlegroups.com
Hi

I have a problem with geom_text. For example following code works:

qplot(x = cyl, y = disp, data = mtcars) + geom_text(data =
data.frame( x = 6, y= 10), map=aes(x=x, y=y), label = "Text")

However, adding a colour variable breaks the code. For example:

> qplot(x = cyl, y = disp, data = mtcars, colour = cyl) + geom_text(data = data.frame( x = 6, y= 10), map=aes(x=x, y=y), label = "Text")
Error in eval(expr, envir, enclos) : object 'cyl' not found

It seems that it does not want to create new layer for text, even
though I have explicitly created new data frame for it. Is there some
way I can avoid the problem?

Best,
Raivo

baptiste auguie

unread,
Aug 19, 2009, 9:01:38 AM8/19/09
to Raivo Kolde, ggp...@googlegroups.com
Hi,

I believe the problem is that the new layer inherits the aesthetic
mapping for colour defined in the qplot() call, i.e. it looks for a
variable cyl in the data.frame supplied to geom_text and fails to find
one. You can specifically tell geom_text not to bother with this
mapping, or use annotate,

qplot(x = cyl, y = disp, data = mtcars, colour = cyl) +
geom_text(data = data.frame( x = 6, y= 10), map=aes(x=x, y=y),
colour="black", label = "Text")

qplot(x = cyl, y = disp, data = mtcars, colour = cyl) +
annotate(geom="text", x = 6, y= 10, colour="black", label = "Text" )

HTH,

baptiste
--
_____________________________

Baptiste Auguié

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

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

Raivo Kolde

unread,
Aug 19, 2009, 9:25:57 AM8/19/09
to baptiste auguie, ggp...@googlegroups.com
Thanks! I spent quite a bit of trying to get this working. Wish I had
known about the function annotate before.

Raivo


2009/8/19 baptiste auguie <bapt...@googlemail.com>:

Perry

unread,
Aug 19, 2009, 9:47:24 AM8/19/09
to ggplot2
I've seen this before, too. If you specify colour in the first aes
mapping, then you have to specify it in all aes that you use later. So

qplot(x=cyl,y=disp,data=mtcars,colour=cyl) + geom_text(data=data.frame
(x=6,y=10),map=aes(x=x,y=x,colour=NULL),label="Text")

works because you satisfy the inherited structure of the aes.

Perry

On Aug 19, 9:25 am, Raivo Kolde <rko...@gmail.com> wrote:
> Thanks! I spent quite a bit of trying to get this working. Wish I had
> known about the function annotate before.
>
> Raivo
>
> 2009/8/19 baptiste auguie <bapt4...@googlemail.com>:
Reply all
Reply to author
Forward
0 new messages