How annotate specific facets and get expressions into facet strip text?

2,809 views
Skip to first unread message

David Lovell

unread,
Mar 1, 2009, 7:29:37 PM3/1/09
to ggplot2
Dear ggplotters,
First, a huge thank you to you Hadley for conceiving and implementing
such an elegant system for visualising data. I think you have created
something that will grow in popularity and impact and be of immense
benefit to those of us who explore and present data.

I'm asking this question as a newcomer to ggplot2 who has looked
through the latest book draft and the discussions in this group, but
so far, failed to find how to annotate specific facets or get
expressions into facet strip text.

Using the following data:
design <- expand.grid(factor1=paste("Level", letters[1:3]),
factor2=paste("Level", letters[4:5]),
replicate=1:4)
example <- data.frame(design,
x=rnorm(design$replicate),
y=rnorm(design$replicate))

...and the following plot:
p <- ggplot(example, aes(x=x, y=y))
p <- p + geom_point() + facet_grid(factor1 ~ factor2)
p + opts(title=expression(paste("It's not all Gr", epsilon, epsilon,
"k to me, yet"))

...could anyone tell me how to
1. Change the strip text for factor 1 to display the Greek letters
alpha, beta, gamma instead of "a", "b", "c"?
2. Annotate the panel corresponding to levels (a,d) with some text?
3. Annotate a point within a specific panel?

I'd be most grateful.
With many thanks in advance,
David

hadley wickham

unread,
Mar 4, 2009, 11:15:03 AM3/4/09
to David Lovell, ggplot2
On Sun, Mar 1, 2009 at 6:29 PM, David Lovell <David....@csiro.au> wrote:
>
> Dear ggplotters,
> First, a huge thank you to you Hadley for conceiving and implementing
> such an elegant system for visualising data. I think you have created
> something that will grow in popularity and impact and be of immense
> benefit to those of us who explore and present data.

Thanks :)

> ...could anyone tell me how to
> 1. Change the strip text for factor 1 to display the Greek letters
> alpha, beta, gamma instead of "a", "b", "c"?

It's not possible in the current version, but I just made a change in
the development version, so you'll be able to do something like

mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2, labeller = label_parsed)

to have the labels show up as their greek letters.

> 2. Annotate the panel corresponding to levels (a,d) with some text?
> 3. Annotate a point within a specific panel?

Just construct an appropriate data frame that records where (in which
panel and in which location) you want the labels, and then use
geom_text() to plot it. If you have a specific problem, I can provide
more details, otherwise have a look at my example with the presidents
data in the toolbox chapter.

Regards,

Hadley

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

David Lovell

unread,
Mar 5, 2009, 7:31:02 AM3/5/09
to ggplot2
On Mar 5, 3:15 am, hadley wickham <h.wick...@gmail.com> wrote:
> Just construct an appropriate data frame that records where (in which
> panel and in which location) you want the labels, and then use
> geom_text() to plot it.  If you have a specific problem, I can provide
> more details, otherwise have a look at my example with the presidents
> data in the toolbox chapter.

Oh, I get it... as in

factor1 <- paste("Level", letters[1:3])
factor2 <- paste("Level", letters[4:5])
design <- expand.grid(factor1=factor1, factor2=factor2,
replicate=1:4)
example <- data.frame(design, x=rnorm(design$replicate), y=rnorm(design
$replicate))
xmax <- max(example$x)
ymax <- max(example$y)

factor1x2 <- expand.grid(factor1=paste("Level", letters[1:3]),
factor2=paste("Level", letters[4:5]))

annotation <- data.frame(factor1x2, x=xmax, y=ymax,
label=paste(factor1x2[[1]], factor1x2[[2]]))

p <- ggplot(example, aes(x=x, y=y))
p <- p + geom_point() + facet_grid(factor1 ~ factor2)
p + geom_text(aes(label=label), data=annotation, hjust = 1, vjust = 1)

...how splendid! You're a star HW. Many (more) thanks.
David

hadley wickham

unread,
Mar 5, 2009, 9:28:28 AM3/5/09
to David Lovell, ggplot2

Exactly :) Once I figure out what the common patterns of annotations
are, I plan to provide a few helper functions to make annotating your
plot a little easier.

Hadley

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

Keaven Anderson

unread,
Sep 17, 2012, 11:39:05 AM9/17/12
to ggp...@googlegroups.com, david....@csiro.au
I am trying to label facets with "alpha-spending" and "beta-spending" where alpha and beta are replaced with greek characters.
No success yet

Keaven Anderson

unread,
Sep 17, 2012, 12:01:04 PM9/17/12
to ggp...@googlegroups.com, david....@csiro.au
The key to making this happen turned out to be:

xx$Bound <- factor(xx$bound,labels=c(paste(expression("alpha"),"-spending"),
                                     paste(expression("beta"), "-spending")))

which could could then be used in facet_grid as already documented here:

... + facet_grid(. ~ Bound, labeller=label_parsed)

On Sunday, March 1, 2009 7:29:37 PM UTC-5, David Lovell wrote:

Hadley Wickham

unread,
Sep 17, 2012, 9:30:23 PM9/17/12
to Keaven Anderson, ggp...@googlegroups.com, david....@csiro.au
On Mon, Sep 17, 2012 at 11:01 AM, Keaven Anderson <kea...@gmail.com> wrote:
> The key to making this happen turned out to be:
>
> xx$Bound <- factor(xx$bound,labels=c(paste(expression("alpha"),"-spending"),
> paste(expression("beta"),
> "-spending")))

Which I'm pretty sure is equivalent to

xx$Bound <- factor(xx$bound ,labels = c("alpha-spending", "beta-spending"))

Hadley


--
RStudio / Rice University
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages