How to do ggplot2 facet_wrap on different pages?

Viewed 1142

I would need to plot 25 violin plots on different pages.

ggplot(sum.tab_tall,aes(x=variable,y=value,fill=Genes))+
  geom_violin()+
  theme(axis.text.x = element_text(angle = 90))+
  facet_wrap(~Genes,scales = 'fixed', nrow=3)

With this code, I obtain the 25 plots on the same page and they are impossible to read. How can I separate them into different pages?

Plot obtained

I already tried with face_wrap_paginate but I was not successful.

ggplot(data=df, aes(x=variable,y=value,fill=Genes)) +
  geom_violin(position=position_dodge2(preserve = 'single')+
   facet_wrap_paginate(~c("LRP1","SCRG1","ENTPD1"), ncol = 3, nrow = 3,
                      scales = "fixed", strip.position = "top", page = 3))                                 
> dput(summary.table)
structure(list(Genes = c("LRP1", "LRP1", "LRP1", "COLEC12", "COLEC12", 
"COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1", 
"CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151", 
"CD276", "CD276", "CD276"), CAF.unstimulated.6h = c(12.1936, 
12.0941, 12.0306, 11.1081, 10.9277, 11.23, 5.62037, 5.61798, 
5.49475, 10.218, 10.0528, 10.1973, 12.6661, 12.5592, 12.5659, 
10.9451, 10.853, 10.9677, 8.85865, 8.79687, 8.83243), CAF.unstimulated.48h = c(12.1592, 
12.2532, 12.2616, 9.03901, 9.47515, 9.26094, 5.25498, 5.39444, 
5.24641, 9.55221, 9.62914, 9.44706, 12.6049, 12.6259, 12.625, 
10.8893, 10.9459, 10.9017, 8.86994, 8.68394, 8.86783), CAF.PDGFCC.stimulated.6h = c(12.0032, 
12.0774, 12.0971, 10.8014, 10.9064, 11.046, 5.77506, 5.51088, 
5.4302, 10.5959, 10.5748, 10.5775, 12.6446, 12.611, 12.675, 10.8996, 
10.8724, 10.9116, 9.21853, 9.1611, 9.04555), CAF.PDGFCC.stimulated.48h = c(12.3216, 
12.2989, 12.212, 9.62478, 9.76262, 9.62223, 5.46608, 5.29513, 
5.26186, 10.0617, 10.0577, 10.0604, 12.6986, 12.7245, 12.7018, 
10.7661, 10.7381, 10.8173, 9.04076, 9.02821, 9.20451)), class = "data.frame", row.names = c(NA, 
-21L))
1 Answers

I'm not sure why facet_wrap_paginate didn't work for you, there might be something wrong with the installation of the packages, however you could do it with ggplot2 only (without ggforce)

Since you didn't post a data sample in your question, I'm using the diamond dataset.

Bellow are two options to have a 'pagified' facet wrap, one without ggforce::facet_wrap_paginate and one with it.

Edit I assumed you want to plot the long (melted) form of the data sample you provided.

The data sample:

summary.table <- structure(list(Genes = c("LRP1", "LRP1", "LRP1", "COLEC12", "COLEC12", 
                         "COLEC12", "ENTPD1", "ENTPD1", "ENTPD1", "CMKLR1", "CMKLR1", 
                         "CMKLR1", "CANX", "CANX", "CANX", "CD151", "CD151", "CD151", 
                         "CD276", "CD276", "CD276"), 
                         CAF.unstimulated.6h = c(12.1936, 12.0941, 12.0306, 11.1081, 10.9277, 
                                                 11.23, 5.62037, 5.61798, 5.49475, 10.218, 
                                                 10.0528, 10.1973, 12.6661, 12.5592, 12.5659, 
                                                 10.9451, 10.853, 10.9677, 8.85865, 8.79687, 
                                                 8.83243), 
                         CAF.unstimulated.48h = c(12.1592, 12.2532, 12.2616, 9.03901, 9.47515, 
                                                  9.26094, 5.25498, 5.39444, 5.24641, 9.55221,
                                                  9.62914, 9.44706, 12.6049, 12.6259, 12.625, 
                                                  10.8893, 10.9459, 10.9017, 8.86994, 8.68394, 8.86783), 
                         CAF.PDGFCC.stimulated.6h = c(12.0032, 12.0774, 12.0971, 10.8014, 10.9064, 11.046, 5.77506, 5.51088, 
                                                      5.4302, 10.5959, 10.5748, 10.5775, 12.6446, 12.611, 12.675, 10.8996, 
                                                      10.8724, 10.9116, 9.21853, 9.1611, 9.04555), 
                         CAF.PDGFCC.stimulated.48h = c(12.3216, 12.2989, 12.212, 9.62478, 9.76262, 9.62223, 5.46608, 5.29513, 
                                                       5.26186, 10.0617, 10.0577, 10.0604, 12.6986, 12.7245, 12.7018, 
                                                       10.7661, 10.7381, 10.8173, 9.04076, 9.02821, 9.20451)), 
                         class = "data.frame", row.names = c(NA, -21L))

Without facet_wrap_paginate

library(ggplot2)
library(dplyr)
# save all groups you want to facet by
all_groups <- unique(summary.table_melted$Genes)

n_all_groups <- length(all_groups)
n_col <- 2
n_row <- 2

# split the groups so that you'd have n_col*nrow groups in each split 
start_idx <- seq(1, n_all_groups, n_col*n_row)

group_splits <- lapply(start_idx, 
       function(i){
         all_groups[i:(i+ n_col*n_row -1)]
})

# now for each group split filter the data and create a plot
i <- 0
for(groups in group_splits){
  i <- i + 1
  p <- summary.table_melted %>%
    filter(Genes %in% groups) %>% 
    ggplot() +
    geom_violin(aes(variable, value))+
    theme(axis.text.x = element_text(angle = 90))+
    facet_wrap(~ Genes, ncol = 3, nrow = 3)
  ggsave(plot = p, filename = paste0('Downloads/page_', i, '.jpg'))
}
  

With facet_wrap_paginate

This is the same example posted in the documentation of facet_wrap_paginate but with a for loop and ggsave to save all pages:

library(ggplot2)
library(ggforce)

summary.table_melted <- reshape2::melt(summary.table, id.vars='Genes')
p <- ggplot(summary.table_melted) +
  geom_violin(aes(variable, value))+
  theme(axis.text.x = element_text(angle = 90))+
  facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2)

for(i in 1:n_pages(p)){
  p_save <-  p + 
    facet_wrap_paginate(~ Genes, ncol = 2, nrow = 2, page = i)
  ggsave(plot = p_save, filename = paste0('Downloads/page_', i, '.jpg'))
}

Related