Tricky combining of two facets with grid.arrange()

1,032 views
Skip to first unread message

Johannes Radinger

unread,
May 9, 2012, 11:16:28 AM5/9/12
to ggp...@googlegroups.com
Hi,

I'd like to combine two facets created with
ggplot. The first one (p1) has two colums
and one row and x and y axis. The second
plot (p2) has similarly two columns (the same
groups as columns of p1) and three rows.
p2 should be arranged below p1. Thus p2 doesn't
need facet_labels on the x axis (column labels).
Furthermore, p2 doesn't need any axis.text, labels
and ticks in my case. So I removed them via the opts-settings.

When I want to arrange the plots I run into problems,
because of the missing axis of p2. The plots are
not properly arranged... Is there a simple way to
arrange plots by their box?

Here is an example explaining the setup (pls
forget any meaning of the data...it is just to explain
how my plots look like):


p1 <- ggplot(mtcars[mtcars$cyl!=8,], aes(mpg, wt))+
geom_point()+
facet_wrap( ~ cyl)

p2 <- ggplot(mtcars[mtcars$cyl!=8,], aes(mpg, wt))+
geom_point()+
facet_grid(am ~ cyl)+
opts( axis.text.y = theme_blank(),
axis.text.x = theme_blank(),
axis.title.y = theme_blank(),
axis.ticks = theme_blank(),
#strip.background = theme_blank(),
strip.text.x = theme_blank())

grid.arrange(arrangeGrob(p1,p2,
main ="this is a title",
left = "This is my global Y-axis title"))


Any suggestion and help is mostly welcome.

Best regards,
Johannes
--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

baptiste auguie

unread,
May 9, 2012, 4:54:34 PM5/9/12
to Johannes Radinger, ggp...@googlegroups.com
Hi,

You don't need both arrangeGrob() and grid.arrange(), one is enough.
The alignment problem is a long-standing, unresolved issue. Kohske has
an experimental branch to solve it that you can try. Other than that,
you'd have to use some hack such as the now defunct align.plots() that
was in ggExtra. I'm not aware of a version that works with facets and
with the most recent ggplot2, unfortunately. You could probably get
some inspiration from this gist and adapt it for multiple panels,

https://gist.github.com/2380653

HTH,

baptiste
> --
> 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
Reply all
Reply to author
Forward
0 new messages