Home > Software design >  Titles for each wrapped plots created from a list of dataframes
Titles for each wrapped plots created from a list of dataframes

Time:05-25

I have a list of dataframes (datalist) and I have created wrapped plots with geom_line in ggplot however I am struggling to assign to each plot its title (BA0, OG0, ON0, etc.).

List of 27  
$ BA0  :'data.frame':   14587 obs. of  2 variables:
   ..$ V1  : int [1:14587] 1 2 1 1 2 1 2 1 1 1 ...
   ..$ V2  : int [1:14587] 43 45 46 48 49 53 55 56 57 58 ...

  $ OG0   :'data.frame':    7925 obs. of  2 variables:
   ..$ V1  : int [1:7925] 1 1 1 1 1 2 5 7 4 10 ...
   ..$ V2  : int [1:7925] 43 53 84 88 90 91 92 93 94 95 ...

  $ ON0   :'data.frame':    8347 obs. of  2 variables:
   ..$ V1  : int [1:8347] 1 2 10 6 6 3 11 6 6 6 ...
   ..$ V2  : int [1:8347] 96 97 98 99 100 101 102 103 104 105 ...

Here's the code

names(datalist) <- c("BA0", "OG0", "ON0", ...)

graph<-lapply(datalist,function(x)      
  p<-ggplot(x,aes(x= V2,y= V1))  
    geom_line(colour = "blue")   
    labs(x = "read length", y="occurences")  
    scale_x_continuous(n.breaks =10)  
    scale_y_continuous(n.breaks = 8) 
) 
wrap_plots(graph)

I have tried to add to p the options:

ggtitle(datalist$file) ## here I added an extra column to each df corresponding to the names: BA0, OG0, ON0 etc..
ggtitle(names(datalist))
labs(title = names(datalist))

But I only get to have the same title for each plots, BA0, which is the first element of the list

enter image description here

How can I add the correct titles? Or maybe there is a better way to create this serie of plots?

Thanks

CodePudding user response:

Using facet_wrap might be suitable. You can try

library(data.table)
library(ggplot2)

#from list to dataframe, adding name of list item as column
dt <- rbindlist(datalist, idcol = TRUE)

#plot using facet_wrap
ggplot(dt,aes(x= V2,y= V1))  
  geom_line(colour = "blue")   
  labs(x = "read length", y="occurences")  
  scale_x_continuous(n.breaks =10)  
  scale_y_continuous(n.breaks = 8)  
  facet_wrap(~.id)

CodePudding user response:

You can enframe the list of data frames into a table and create a new column title using the function dplyr::mutate. Then you can use purrr::pmap to map elements of columns to a function formula creating the plot. Lastly, the plot column can be feed into patchwork::wrap_plots:

library(tidyverse)
library(patchwork)

data <-
  list(
    iris1 = iris,
    iris2 = iris
  )

str(data)
#> List of 2
#>  $ iris1:'data.frame':   150 obs. of  5 variables:
#>   ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#>   ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>   ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#>   ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>   ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>  $ iris2:'data.frame':   150 obs. of  5 variables:
#>   ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#>   ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>   ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#>   ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>   ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

data %>%
  enframe() %>%
  mutate(
    title = paste0("Fancy title ", name),
    plt = list(title, value) %>% pmap(~ {
      .y %>%
        ggplot(aes(Sepal.Length, Sepal.Width))  
        geom_line()  
        labs(title = .x)
    })
  ) %>%
  pull(plt) %>%
  wrap_plots()

Created on 2022-05-25 by the reprex package (v2.0.0)

Alternatively, one can also create an explicit function for plotting:

my_plot <- function(title, data) {
  data %>%
    ggplot(aes(Sepal.Length, Sepal.Width))  
    geom_line()  
    labs(title = title)
}

data %>%
  enframe() %>%
  mutate(
    title = paste0("Fancy title ", name),
    plt = list(title, value) %>% pmap(my_plot)
  )
#> # A tibble: 2 × 4
#>   name  value          title             plt   
#>   <chr> <list>         <chr>             <list>
#> 1 iris1 <df [150 × 5]> Fancy title iris1 <gg>  
#> 2 iris2 <df [150 × 5]> Fancy title iris2 <gg>
  • Related