1

I'm trying to do two ggplots of a list of dataframes

I have this list:

list.function <-  function() { 
  
  sample1 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(1, 1, 1))
  sample2 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(2, 2, 2))
  sample3 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(3, 3, 3))
  sample4 <- data.frame(gene_biotype= c("protein_coding", "lncRNA", "intronic"), counts = c(4, 4, 4))
  
  sapply(paste('sample', seq(1,4,1), sep=''), get, environment(), simplify = FALSE) 
} 

my.list3 <- list.function()
my.list3

And I want to do these two plots


a = ggplot(sampleX, aes(y=count, x = gene_biotype, fill = gene_biotype)) + geom_bar(stat = "identity") +
  xlab("Groups") + 
  ylab("Counts") +
  theme_classic() +
  ggtitle(paste0(samplenumber))

b = ggplot(sampleX, aes(y=count, x = gene_biotype, fill = gene_biotype)) + geom_bar(stat = "identity") +
  xlab("Groups") + 
  ylab("Counts") +
  theme_classic() +
  ggtitle(paste0(samplenumber))

png(samplename, width = 10, height = 5, units = "in", res=200)
ggarrange(a, b)
dev.off()

And then print each graph into a png

Gabriel G.
  • 555
  • 1
  • 3
  • 13

2 Answers2

2

You can use purrr::imap to get list of plots :

library(ggplot2)

plot_list <- purrr::imap(my.list3, ~{
  ggplot(.x, aes(y=count, x = gene_biotype, fill = gene_biotype)) + 
    geom_bar(stat = "identity") +
    xlab("Groups") + 
    ylab("Counts") +
    theme_classic() +
    ggtitle(.y)
})

Note that imap is a convenient way to get access to list as well as it's name. You can also do that using apply family of function in base R. For example, with Map :

plot_list <- Map(function(p, q) {
  ggplot(p, aes(y=count, x = gene_biotype, fill = gene_biotype)) + 
      geom_bar(stat = "identity") +
      xlab("Groups") + 
      ylab("Counts") +
      theme_classic() +
      ggtitle(q)
  },my.list3, names(my.list3))

To write each plot in a pdf I think you can use code from here - Printing multiple ggplots into a single pdf, multiple plots per page

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

Hopefully this solution can be adapted to suit your needs:

library(tidyverse)
library(ggpubr)

for (f in seq_along(my.list3)) {
  a <- ggplot(data = my.list3[[f]], aes(y = counts,
                               x = gene_biotype,
                               fill = gene_biotype)) +
    geom_bar(stat = "identity") +
    xlab("Groups") + 
    ylab("Counts") +
    theme_classic() +
    ggtitle(names(my.list3[f]))
  
  b <- ggplot(data = my.list3[[f]], aes(y = counts,
                                        x = gene_biotype,
                                        fill = gene_biotype)) +
    geom_bar(stat = "identity") +
    xlab("Groups") + 
    ylab("Counts") +
    theme_classic() +
    ggtitle(names(my.list3[f]))
    ggsave(filename = paste(names(my.list3[f]), ".png", sep = ""),
          plot = ggarrange(a, b, ncol = 1, nrow = 2, labels = c("A", "B")),
          device = "png", width = 10, height = 5, units = "in")
}

Edit: updated ggsave() options per comment

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • I'm trying to use png(filename = paste(names(named.list6[f])), width = 10, height = 5, units = "in", res=200) ggarrange(a, b, ncol = 1, nrow = 2, labels = c("A", "B")) dev.off() but my code ends up with no .png in the end of each file and no images also. Any ideas? – Gabriel G. Oct 05 '20 at 16:37
  • I have updated my example to include those options. If you run this 'minimal reproducible example' do you get the correct output? – jared_mamrot Oct 06 '20 at 00:43