0

I am trying to build a loop for() to draw & save a ggplot for a list of genes in a dataset. My initial code is the following:

for(i in genelist){
  plot <- ggplot() +
           geom_density(data = mydata, aes(x=log2(i)), fill="pink", alpha = 0.5) +
           xlab(paste("log2 ", i))
  ggsave(plot, filename = paste("plot_",i ,".png"))
  }

genelist is a chr vector.

mydata is a dataframe where colnames are names of genes, rownames are patients barcodes and matrix is count values.

I obtained the following error:

Saving 11.3 x 5.98 in image Error in log2(i) : non-numeric argument to mathematical function

Since the error seems to come from the plot saving, I tried the code without:

for(i in genelist){
  plot <- ggplot() +
           geom_density(data = mydata, aes(x=log2(i)), fill="pink", alpha = 0.5) +
           xlab(paste("log2 ", i))
   }

The loop went to the end without error (i = my last gene in genelist) but when I tried to display the plot:

Error in log2(i) : non-numeric argument to mathematical function

What am I doing wrong?

Sebrw
  • 47
  • 7
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. But the problem most likely is you trying to use `i` in the `aes()` which is lady evaualted. Maybe `aes(x=log2(.data[[i]]))` would work. Also use `paste0()` rather than `paste()` to make sure your file names don't have spaces. – MrFlick Apr 13 '20 at 06:32
  • That works great, thanks! Could you please explain me .data[[i]] syntax? In my limited knowledge of R, [[ ]] is used to subset a list. Does it mean that .data[[i]] tells to take data from the list "i" and since "i" is a gene name corresponding to a column of mydata (dataframe columns are lists, right?) it returns its values? Why is does not work with [[i]] only? – Sebrw Apr 13 '20 at 09:21
  • `.data` is a "magic" variable that ggplot creates for you when plotting. It makes it easier to subset the data via character value just as you are trying to do. It's a way to avoid using the `!!sym()` method. – MrFlick Apr 13 '20 at 17:43

1 Answers1

0

We can write a function :

library(ggplot2)

plot_save <- function(i) {
    plot <- ggplot() +
             geom_density(data = mydata, aes(x=log2(!!sym(i)), 
                          fill="pink", alpha = 0.5)) +
             xlab(paste("log2 ", i))

    ggsave(plot, filename = paste0("plot_",i ,".png"))
}

and pass genelist to the function.

lapply(genelist, plot_save)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213