Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function to ggplot through list of dataframes: add title

I have written a function to plot a list of dataframes.

library(dplyr)
library(tidyr)
library(ggplot2)
#function to plot each df in list
func.plot <- function(x){

ggplot(data = x, aes(x = month)) + 
    geom_line(aes(y = MFI, group = sample), color = "lightgrey", size = 0.5)+ 
    geom_line(aes( y = mean), color = "red", size = 2) + 
    theme_bw()+ 
    ggtitle("for non-recurrent infections")
  }

#apply function to list
lapply(test, func.plot)

I am struggling to get dataframe-specific titles for each plot. I would like each plot to be titled "DATAFRAME NAME for non-recurrent infections".

I have tried a few ideas for the ggtitle line such as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    ggtitle(x, "for non-recurrent infections")

    ggtitle(paste0(x), "for non-recurrent infections")

However these are not working. Any help would be appreciated.

Also – any tips for printing the outputs side by side (perhaps 8 to a plot) would also be appreciated!

SAMPLE DATA:

CSP1: 

structure(list(sample = c(35L, 35L, 35L, 35L, 35L, 35L), month = c(0, 
0.25, 1, 2, 3, 4), MFI = c(228.6666667, 160.6666667, 226.6666667, 
131.6666667, 170.1666667, 115.1666667), mean = c(1013.04700847142, 
1499.62393155462, 697.698717902436, 437.448717901244, 599.012820505269, 
619.978632424128)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -6L), groups = structure(list(
    month = c(0, 0.25, 1, 2, 3, 4), .rows = structure(list(1L, 
        2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))

PvRBP2b: 
    structure(list(sample = c(35L, 35L, 35L, 35L, 35L, 35L), month = c(0, 
0.25, 1, 2, 3, 4), MFI = c(18823, 22188, 15356, 7536, 5372, 2177.5
), mean = c(10131.0427347736, 15102.9273501617, 11751.3803417424, 
11039.9935895637, 11553.0448715292, 9797.237179345)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    month = c(0, 0.25, 1, 2, 3, 4), .rows = structure(list(1L, 
        2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))

test <- list(CSP1, PvRBP2b)

>Solution :

One option would be to use a named list (for which I use dplyr::lst), then use purrr::imap which passes the name as a second argument to your function and of course add a second argument to your plotting function:

library(dplyr, warn=FALSE)
library(ggplot2)

test <- dplyr::lst(CSP1, PvRBP2b)

func.plot <- function(x, name) {
  ggplot(data = x, aes(x = month)) +
    geom_line(aes(y = MFI, group = sample), color = "lightgrey", size = 0.5) +
    geom_line(aes(y = mean), color = "red", size = 2) +
    theme_bw() +
    ggtitle(paste(name, "for non-recurrent infections"))
}

purrr::imap(test, func.plot)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> $CSP1

#> 
#> $PvRBP2b

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading