Home > OS >  Add two labels in ggarrange in R
Add two labels in ggarrange in R

Time:06-22

I would like to have two labels in ggarrange, the first column should be b_1 and the second b_2. enter image description here. The code I use is: Any idea?

figure <- ggarrange(p, r, g ,s, k ,t,
                    labels = c("A", "D", "B", "F", "C", "G"),
                    ncol = 2, nrow = 3)
annotate_figure(figure,
                bottom = text_grob(expression("b"[2]), color = "black", face = "bold", size = 14)
)

CodePudding user response:

It's not easy to do this within annotate_figure. However, a neat alternative is to create two little text-only ggplots:

ann1 <- ggplot()   
  geom_text(aes(x=0, y=0, label = "bold(b[1])"), 
            parse = TRUE, size = 6, hjust = -1)  
  theme_void()

ann2 <- ggplot()   
  geom_text(aes(x=0, y=0, label = "bold(b[2])"), 
            parse = TRUE, size = 6, hjust = -1)  
  theme_void()

Now just add these to the bottom of your grid, ensuring their height is reduced relative to the actual plots:

ggarrange(p, r, g ,s, k ,t, ann1, ann2,
          labels = c("A", "D", "B", "F", "C", "G", "", ""),
          ncol = 2, nrow = 4, heights = c(0.3, 0.3, 0.3, 0.1))

enter image description here


Data used

library(ggpubr)

p <- r <- g <- s <- k <- t <- ggplot(data.frame(x = rnorm(2000)), aes(x))  
  geom_histogram(bins = 10, fill = "#76b8a9", color = "gray95")  
  geom_vline(xintercept = 0, color = "blue2", size = 1, linetype = 2)
  • Related