Facet wrap labels

199 views
Skip to first unread message

Gretchen H

unread,
Jun 22, 2010, 7:20:22 PM6/22/10
to ggplot2
Hello-
I am using facet_wrap to plot multiple histograms by a variable called
"group1". However, I want to label each plot by a different variable
("species"). I can't facet by species, because some species appear
more than once (i.e., belong to more than one group). I can't figure
out how to change the labels on the individual plots- must they be the
faceting variable?

Example:

library(ggplot2)
group1 <- sample(c("1", "2", "3", "4"), 100, replace=T)
species <- sample(c("x", "y"), 200, replace=T)
myvalues <- rnorm(400)
df <- data.frame(group1, species, myvalues)


m<- ggplot(df, aes(myvalues) )
m<-m + geom_histogram(aes(y=..density..))
m<-m+ facet_wrap(~group1)
print(m)

this creates the histograms as I want them, grouped how I want them. I
just can't change the labels to species, and I don't want to resort to
doing it by hand!

Thanks,
Gretchen

Dennis Murphy

unread,
Jun 24, 2010, 1:38:12 AM6/24/10
to Gretchen H, ggplot2
Hi:

According to your example,
> with(df, table(group1, species))
      species
group1  x  y
     1 46 50
     2 50 38
     3 64 56
     4 36 60

So, are you asking for something like

m<- ggplot(df, aes(myvalues) )
m<-m + geom_histogram(aes(y=..density..))
m + facet_grid(species ~ group1)

??
HTH,
Dennis


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

Gretchen Hansen

unread,
Jun 24, 2010, 12:38:25 PM6/24/10
to Dennis Murphy, ggp...@googlegroups.com

Hi

Sorry, in my effort to make the example more simple than my real problem, I obscured what the actual problem was.  In my actual dataset, I have 70 groups.  The groups represent different species collected using different methods.  So, some species are represented by only one group, while others are represented by multiple groups.  I want separate histograms for each group, but I want to label them by species.  The figure produced using facet_wrap(~group1) shows the data exactly as I want them, I just want to replace the labels with the species names (instead of the group number) without having to do it by hand.

Maybe I just need to change all the groups to something containing the species name, but it would be much easier to just change the labels if I can!

Thanks

Gretchen

Hadley Wickham

unread,
Jun 25, 2010, 11:03:26 AM6/25/10
to Gretchen Hansen, Dennis Murphy, ggp...@googlegroups.com
Hi Gretchen,

If there's a one-to-one correspondence between species and group, then
I don't see why you can't facet by species. If there isn't a
one-to-one correspondence, then how do you figure out the species
label?

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Gretchen Hansen

unread,
Jun 25, 2010, 11:09:19 AM6/25/10
to Hadley Wickham, Dennis Murphy, ggp...@googlegroups.com
Hadley-
There is not a one-to-one correspondence in all cases. A few species are
represented by more than one group, so they would have more than one
histogram. I think I just need to rename my groups. I am gathering that it
is not possible to re-label figures created using faceting with anything
other than the variable used to facet them, which is really the core of my
question, is this correct?
Thanks a lot for the help!
Gretchen

Hadley Wickham

unread,
Jul 3, 2010, 9:17:07 AM7/3/10
to Gretchen Hansen, Dennis Murphy, ggp...@googlegroups.com
> There is not a one-to-one correspondence in all cases. A few species are
> represented by more than one group, so they would have more than one
> histogram.  I think I just need to rename my groups.  I am gathering that it
> is not possible to re-label figures created using faceting with anything
> other than the variable used to facet them, which is really the core of my
> question, is this correct?

You can, using a customised labeller function, but it's easier just to
create new variable (and, regardless, I still don't see how it would
help with your problem).

Hadley

Reply all
Reply to author
Forward
0 new messages