Ggplot2 aligns the top of two facets

I need to arrange two faceted graphs, for example:

d = data.frame(Index = LETTERS[1:5],x=1:5,y=1:5)
A = ggplot(subset(d,Index == 'A'),aes(x,y)) + 
  theme_bw() + 
  theme(axis.title.x = element_blank()) + 
  geom_point() + facet_wrap(~Index) + labs(title = "Title, The Title", 
                                           subtitle = "Subtitle, The Subtitle",
                                           y = "Side Axes")
B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
  theme_bw() + 
  theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + 
  geom_point() + facet_wrap(~Index) + labs(title = "", subtitle = "")
g = gridExtra::arrangeGrob(A,B,ncol=2,bottom="Bottom Axes")
grid.arrange(g)

Which gives the following:

Conclusion

As can be seen from the above, there is a slight misalignment between the upper edges of the plot area. This is caused by commas in the title and subtitles.

Does anyone know how I can ensure the alignment of the top edges? I need a title and subtitles on the left chart, with a (empty) title, subtitles on the right.

+4
source share
3 answers

@CephBirk - . , , , , grid.arrange arrangeGrob, nullGrob() A B, x- (1.50) .

library(gridExtra)

A = ggplot(subset(d,Index == 'A'),aes(x,y)) + 
  theme_bw() + 
  theme(axis.title = element_blank()) + 
  geom_point() + facet_wrap(~Index) 

B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
  theme_bw() + 
  theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + 
  geom_point() + facet_wrap(~Index) 

grid.arrange(
  arrangeGrob(
    arrangeGrob(textGrob("Title, The Title", hjust=0), 
                textGrob("Subtitle, The Subtitle", hjust=0, gp=gpar(cex=0.8))),
    nullGrob(), ncol=2, widths=c(1,4)),
  arrangeGrob(A, nullGrob(), B, ncol=3, widths=c(8,0.1,8),
              left="Side Axes", bottom="Bottom Axes"), 
  heights=c(1,12))

enter image description here

+4

, , , .:) - , .

, .

B = ggplot(subset(d,Index != 'A'),aes(x,y)) +
theme_bw() + 
theme(axis.title.x = element_blank(), axis.title.y = element_blank(), title = element_text(color = 'white')) + 
geom_point() + facet_wrap(~Index) +
labs(title = "Title, The Title", subtitle = "Subtitle, The Subtitle")
+2
egg::ggarrange(A, B, ncol=2, bottom="Bottom Axes")

enter image description here

+2

Source: https://habr.com/ru/post/1663295/


All Articles