geom_text and faceting

3,108 views
Skip to first unread message

Sam

unread,
Nov 22, 2010, 3:13:24 PM11/22/10
to ggplot2
Hello there,

I can't seem to figure out how to construct a plot where I can place
different text within each facet. Basically I can't figure out how to
use geom_text in a way that allows me to specify text for a specific
facet. Would anyone be able to modify the example below to achieve
this?

Thanks in advance!

Sam


library(ggplot2)
x <-runif(9, 0, 125)
data <- as.data.frame(x)
data$y <--runif(9, 0, 125)
data$yy <- factor(c("a","b","c"))

pg <- ggplot(data, aes(x, y)) + geom_point(shape = 2) +
facet_grid(~yy) + geom_text(aes(80, 30, label="Same for each facet"))
pg

baptiste auguie

unread,
Nov 22, 2010, 3:20:02 PM11/22/10
to Sam, ggplot2
Hi,

Try this,

ggplot(data, aes(x, y)) + geom_point(shape = 2) +

facet_grid(~yy) + geom_text(aes(x, y, label=lab),
data=data.frame(x=60,y=Inf,lab=c("this","is","the way"),
yy=letters[1:3]), vjust=1)

Also, note that your example was overlapping the labels many times
because of your incorrect use of aes().

HTH,

baptiste

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

Malcolm Cook

unread,
Aug 6, 2013, 1:13:12 PM8/6/13
to ggp...@googlegroups.com, Sam, bapt...@googlemail.com
Baptiste, et al,

I have a very similar problem as Sam and wondered if you might have a clue for me.

In my case, I am not using grid_facet but rather assigning the group variable to a color and needing the text for each group to not overlap, which they are as I've currently coded it.

Using the same data as Sam, here is what I have tried:

ggplot(data, aes(x, y)) +
    geom_point(aes(color=yy),shape = 2) +
    geom_text(mapping=
              aes(x
                  ,y
                  , ymax=Inf
                  , label=lab
                  , color=yy            # color the text too
                  , group=yy # saw some hints on using group aesthetic elsewhere
                  )
              ,position='stack'
              ,data=data.frame(
                   x=60
                   ,y=Inf

                   ,lab=c("this","is","the way")
                   ,yy=letters[1:3]
                   )
              ,vjust=1)



Roderick Slieker

unread,
Aug 7, 2013, 11:05:18 AM8/7/13
to ggp...@googlegroups.com
I think this is what you are looking for.

data$yy <- factor(c("this","is","whaturlookingfor"))
ggplot(data, aes(x, y)) +
    geom_point(aes(color=yy,fill=yy),shape = 2)
Reply all
Reply to author
Forward
0 new messages