1

I have a series of plots that I want to combine into one, and I cannot use facet_wrap. So each of my plots is a separate object. I want them all the same size, and I can combine them this way.

p = ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + xlab("") + ylab("")

g=ggpubr::ggarrange(p, p, p,
                    p, p, p,
                    p, p, p, 
                    ncol = 3, nrow = 3)

base plot without wanted labels

I want to add some text labels that would apply to the columns and rows, as shown here.

plot showing desired placement of labels

I have tried adding titles to the individual plots (for example, those on the top row), but that reduces the size of the (grayed) plot compared to the others so that the plot dimensions are no longer all the same size.

Any clues would be greatly appreciated.

M--
  • 25,431
  • 8
  • 61
  • 93
  • 3
    `patchwork` is brand new and should contain some helpful tools - https://patchwork.data-imaginist.com/ You can put plots in a layout and add text grobs around it. – Andy Baxter Dec 03 '19 at 21:25
  • the question is - why can't you use facets... – tjebo Dec 03 '19 at 21:42
  • 2
    @user3196167 As an aside: use `... + xlab(NULL) + ylab(NULL)` if you want to hide axis titles. If you use an empty string, i.e. `""` then ggplot still allocates space for their representation. But not if you use `NULL` instead. – markus Dec 03 '19 at 21:48

1 Answers1

2

You can add secondary axes, clear the title for the primary ones and delete the tick marks for secondary ones but keep the titles (look into the theme function I added to your ggplot object). Then tile of those secondary axes can be used to write your desired text. Look below for an example.

library(ggplot2)
library(ggpubr)


p <- ggplot(mtcars, aes(x=wt, y=mpg)) + 
      geom_point() + 
      xlab("") + ylab("") + 
          scale_y_continuous(position = 'right', sec.axis = dup_axis()) + 
          scale_x_continuous(position = "top", sec.axis = dup_axis()) +
          theme(plot.title = element_text(hjust=0.5), 
                axis.text.x.top = element_blank(),
                axis.ticks.x.top = element_blank(),
                axis.text.y.right = element_blank(),
                axis.ticks.y.right = element_blank(),
                axis.title.x.bottom = element_blank(), 
                axis.title.y.left = element_blank())


ggarrange(p + xlab("some text 1"), 
          p + xlab("some text 2"), 
          p + xlab("some text 3") + ylab("some text 33"),
          p, p, p + ylab("some text44"),
          p, p, p + ylab("some text55"), 
          ncol = 3, nrow = 3)

You can add axis.title.y.right = element_text(angle = 0) into theme to make y-axis title to be horizontal.

In reference to How to keep axis labels in one side and axis title in another using ggplot2

M--
  • 25,431
  • 8
  • 61
  • 93