1

I have a list dat.list of data frames that look like this:

    GeneId  baseMean    log2FoldChange  lfcSE   stat    pvalue  padj    threshold   type    sigtype
    18S_rRNA:18S_rRNA   1209166.39919685    -0.209458177840739  0.234737554962532   -0.892307913295647  0.372227913902729   0.448713101690961   non-sig other   non-sig-other
    28S_rRNA:28S_rRNA   2592930.17340529    -0.180545589669893  0.231050649278924   -0.781411306280031  0.434560623435828   0.513996436321948   non-sig other   non-sig-other
    5.8S_rRNA:5.8S_rRNA 766069.47551055 0.113601345777545   0.191275069218117   0.593916113803629   0.552568220875051   0.615519030848158   non-sig other   non-sig-other
    5S_rRNA:5S_rRNA 2.79711958523178    -1.58909406740344   1.16966935304225    -1.3585839992048    0.174278448339169   0.238886346633129   non-sig other   non-sig-other
    MS2-RNA-control1:MS2-RNA-control1   81986.8654716761    -1.52675604546698   0.617811342665657   -2.47123343329943   0.0134647893998128  0.0283469250522374  sig other   sig-other
    MS2-RNA-control2`:MS2-RNA-control2  4610.31645194732    -4.54922459870619   0.929798724392121   -4.89269825755069   9.94628389868329e-07    4.37636491542065e-06    sig other   sig-other

I want to generate an MA plot for each data frame using this function:

maplot <- function(df) {
  ggplot(df, 
    aes(x = baseMean, y = log2FoldChange)) +
    scale_x_continuous(trans = "log10") +
    geom_point(aes(col = threshold), size = 1, shape = 20) +
    scale_color_manual(values = c("non-sig" = "gray70","sig" = "red")) +
    geom_point(data = df[df$sigtype == 'sig-haca', ], pch = 21, fill = NA, size = 3, colour = "blue", stroke = 0.5) +
    ylim(-5, 10) +
    geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
    xlab("mean of normalized counts") + 
    ggtitle(paste("MAPLOT_", titlename, sep = "")) +
    theme_classic()
}

I have tried lapply(dat.list, maplot), but that doesn't work. How do I specify titlename in the ggtitle call?

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
user3138373
  • 519
  • 1
  • 6
  • 18
  • Can you create a minimal reproducible example? – markus Feb 11 '22 at 21:53
  • I added some more information – user3138373 Feb 11 '22 at 22:00
  • It's better include data with output of `dput(your_data)`, see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – markus Feb 11 '22 at 22:09
  • I added dput output for the functions I made. Please check – user3138373 Feb 11 '22 at 22:20
  • 1
    You need to make `maplot` a function of 2 arguments, `df` and `titlename`, then do `Map(maplot, df = dat.list, titlename = nms)`, with `nms` a vector of names. – Mikael Jagan Feb 12 '22 at 03:18
  • I have deleted irrelevant details from your question. How you generated the list of data frames doesn't matter. What matters is the structure of your data, so it would help to see `dput(head(dat.list[[1L]], 10L))`. Ultimately, your question is just about passing `titlename` to your function `maplot`. – Mikael Jagan Feb 12 '22 at 03:29
  • Thanks for clearing up the question. To pass titlename to the function maplot, should I again use my character vector `files` within the maplot function? Because `files` vector has names of the title in it. I will try to write a 2 argument function. – user3138373 Feb 12 '22 at 05:37
  • You just have to replace `function(df)` with `function(df, titlename)`. You can pass any character vector you want for `titlenames`. It will be recycled to the length of `dat.list`. `files` might make sense, but how you title your plots is up to you. – Mikael Jagan Feb 12 '22 at 06:20
  • Can you tell which package is Map command from? Is it from `purrr` package? The reason I say `files` vector is because the names are present in it. For eg `files <- c("abc.txt","def.txt","xyz.txt")`. Then the titlename I need is ab,def and xyz. Since my dat.list is produced from files in a given order, so the same order will be followed for titlename. Does that sound reasonable? – user3138373 Feb 12 '22 at 06:33
  • why would lapply not work? `lapply(dat.list,maplot,dat.list,nms)`. – user3138373 Feb 15 '22 at 22:11

0 Answers0