Possible to place legend in empty facet?

3,382 views
Skip to first unread message

skipperhoyer

unread,
Sep 28, 2012, 9:11:23 AM9/28/12
to ggp...@googlegroups.com
Hello!

In, for example, the graphic generated below, the facets are arranged in a three-by-three arrangement.  There are 9 facets, so the last facet space is blank. The legend is added to the right of the entire plot, which interferes with the shape of the plots, for a fixed full figure size. Is it possible to place the legend in the blank space?

library(reshape)
library(ggplot2)
dat <- data.frame("class" = as.factor(rep(letters[1:5], each = 10)),
"age" = as.factor(rep(1:5, 10)),
"gender" = sample(c("M", "F"), replace = T, size = 50),
"score" = rnorm(n=50))
ggplot(dat, aes(x = class, y = score, colour = gender)) + geom_point() +
facet_wrap(~age)


Thanks folks!
Benjamin

Kohske Takahashi

unread,
Sep 29, 2012, 2:22:46 AM9/29/12
to skipperhoyer, ggp...@googlegroups.com
try this:

ggplot(dat, aes(x = class, y = score, colour = gender)) + geom_point() +
facet_wrap(~age) +
theme(legend.position = c(1, 0), legend.justification = c(1, 0))



2012/9/28 skipperhoyer <bdh...@gmail.com>:
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
Reply all
Reply to author
Forward
0 new messages