Reputation: 996
In ggplot, it is often the case that I will want to create a multiplot using grid.arrange()
and arrangeGrob()
where axis labels are redundant. Take for example the following figure:
data = data.frame(x=1:50, y=50:1)
p1=ggplot(data, aes(x=x, y=y)) + geom_line() + xlab("Dimension") + ylab("Magnitude")
p2=ggplot(data, aes(x=x, y=-y)) + geom_line() + xlab("Dimension") + ylab("Magnitude")
grid.arrange(p1, p2, nrow=1)
It is clear that the label "Dimenion" and "Magnitude" are redundant on the rightmost plot.
I know i can easily remove the ticks, labels, and titles:
p2=p2 +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
grid.arrange(p1, p2, nrow=1)
But this leaves me with the margins crushed on the second figure. I know I could do this manually by going through scale_x_continuous
and scale_y_continuous
, and instead of just replacing them with blanks like I do via theme()
, I could manually set everything to ""
, but this is time consuming and requires somewhat of a less automated approach to doing it like element_blank()
.
Is there a similarly trivial way to remove the labels, without crushing the space they previously occupied? Something like element blank without the "assigns no space" aspect.
Upvotes: 1
Views: 3720
Reputation: 26343
Like so?
#devtools::install_github("thomasp85/patchwork")
library(patchwork)
p1 + p2
Don't ask me how the magic works. You can read more about the functionality here: github.com/thomasp85/patchwork
Upvotes: 3