This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plot multiple genes from dds data

Hi everyone, I am struggling to find a way to plot the list of genes of my interest in a single pdf file using plotCounts(dds, gene = geneID) function. It just work perfectly when I want a single gene. As soon as I try to plot a list, it does not work. I even tried to use Style="Facet but it did not work. I would appreciate if someone can help me with this issue.

genes <- read.csv("./genes.csv", header=T)
geneID<-genes$igenename    
p<-plotCounts(dds, gene = geneID, intgroup = c("RANKL", "Genotype", "Background"),returnData=TRUE)

ggplot(p, aes(x = Genotype, y = count, color = Background, shape=RANKL)) + 
    geom_point(size=4, position=position_jitter(w = 0.1,h = 0)) +
        theme_bw() +
           theme(plot.title = element_text(hjust = 0.5))
plotcounts

1 answer

The function description states it's only for a single gene, so you need to loop through your gene IDs.

library("DESeq2")

dds <- makeExampleDESeqDataSet()
dds$time <- sample(1:3, ncol(dds), replace=TRUE)

gene_set <- c("gene1", "gene3", "gene10")
names(gene_set) <- gene_set

df <- lapply(gene_set, \(x) {
    y <- plotCounts(dds, x, c("condition", "time"), returnData=TRUE)
    y$feature <- x
    return(y)
})

df <- do.call(rbind, df)

This will give you a properly formatted long data.frame you can use in ggplot2.

Thanks very much for your help. I tried this wonderful code and made the long data.frame but I do not know how I can plot this kind of data frame. I am sorry for this stupid question. Should it be also looped?

You'll need to learn ggplot2 to really get the most out of it since it can make almost any plot type you want. Here's an example.

ggplot(df, aes(x=condition, y=count, color=as.factor(time))) +
  geom_jitter(
    position=position_jitterdodge(dodge.width=0.75),
    size=0.75) +
  geom_boxplot(
    position=position_dodge(width=0.5),
    fill=NA, width=0.25, outlier.shape=NA) +
  facet_grid(feature~.)

enter image description here

Thank you very much for your big help. The problem with facet-grid is that it adds on all the graphs with the same scale. I have some genes which are lowly expressed and this way I do not see the differences clearly. If we could have for every single graph its own scale that would be awesome. I am not sure though if this is possible. Thanks one more time.

Log in to answer this question.