Reputation: 303
I have a list of data tables, each of which contains data in the same format and each of which has a unique name. Example below:
> head(list$table1)
gene logFC negLogPval
1 CROT -1.546082 6.405688
2 CASC15 -1.718302 5.501062
3 ITGA8 -2.839048 5.159019
4 LDB2 -1.258781 4.646456
5 PTGS1 1.009361 3.791273
6 FABP5 -1.186742 3.189549
I want to make a separate plot from each of these data tables in the list and then save, which I can do using the following function:
volc = function(input){
ggplot(input, aes(logFC, negLogPval)) +
geom_point()
ggsave(paste0("Volcano_", names(input), ".png"), device = "png")
}
If I run this on a single data table as below:
volc(list$table1)
Then it generates a plot as expected, but instead of taking the name of the object to name the file, it takes the name of the first column in the data table instead, i.e. saves the file as "Volcano_gene.png" instead of "Volcano_table1.png".
This means that if I run this on my list of data tables using lapply
, as below:
lapply(list, volc)
It runs through the code on each of the tables, but saves all of them as "Volcano_gene.png", overwriting the previous table as it goes, meaning the only plot that gets saved is the very last one generated.
I am sure I am missing something simple with the names()
function, but can anyone help me fix this so that it saves the plots with names as expected?
Upvotes: 1
Views: 789
Reputation: 2722
The problem with your code is that when you apply your function to each data.frame
in your list
then names(input)
gives you the column names of the data.frame
. See this example.
data(mtcars)
l = list()
l$a = mtcars[, 1:2]
l$b = mtcars[, 2:3]
lapply(l, names)
$a
[1] "mpg" "cyl"
$b
[1] "cyl" "disp"
To resolve this change the function to take a second argument name.
volc = function(input, name) {
ggplot(input, aes(logFC, negLogPval)) +
geom_point()
ggsave(paste0("Volcano_", name, ".png"), device = "png")
}
Now you can use Map
to archieve your goal:
Map(volc, list, names(list))
Upvotes: 1
Reputation: 132999
You don't want the column names of the data.frames in that list but their names, i.e., names(list)
. I suggest iterating over these names:
volc = function(inputname){
ggplot(list[[inputname]], aes(logFC, negLogPval)) +
geom_point()
ggsave(paste0("Volcano_", inputname, ".png"), device = "png")
}
for (x in names(list)) volc(x)
Upvotes: 3