This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Boxplot of contrasts and genes together

Hi,

I have 4 strains: A,B,C, D each with mutant and wild samples. I ran differential expression analysis with two designs: 1) design = ~ Condition to get differentially expressed genes for contrast mutant_vs_wild 2) design = ~ Condition_strains to get differentially expressed genes for contrast mutant_A_vs_mutant_B

Now I want to make a boxplot as follows from res object for multiple genes: enter image description here

Please advise. Thanks.

boxplot deseq2 expression

Where are you stuck ? You need to dig into ggplot2 functions

1 answer

The key is to use different aesthetics in ggplot2 to subdivide your data. See this example:

library(ggplot2)
library(stringr)
library(reshape2)

# Generate example expression data
cts <- matrix(rnorm(3*32),3,32)
rownames(cts) <- c("gene1","gene2","gene3")
colnames(cts) <- c(
  rep("A_mutant",4),rep("A_wild",4),
  rep("B_mutant",4),rep("B_wild",4),
  rep("C_mutant",4),rep("C_wild",4),
  rep("D_mutant",4),rep("D_wild",4))

# Tidy-up and build variables
cts <- reshape2::melt(cts)
cts$type <- stringr::str_split_fixed(cts$Var2,"_",2)[,2]
cts$strain <- stringr::str_split_fixed(cts$Var2,"_",2)[,1]

# Select gene
cts.gene1 <- cts[cts$Var1 == "gene1",]
# Plot and fill by strain
ggplot(cts.gene1, aes(x = type, y = value, fill = strain)) + geom_boxplot()

Thanks Papyrus, Basti. I am struggling with how to make the final dataframe from two designs I mentioned which include retrieving counts for the same gene ( I have a list of 20 genes) from multiple contrasts and generate a big figure with all genes together.

The counts for your gene are independent from the contrasts you do, you should retrieve them from your count/expression matrix, which should look like the example I generated for you. I still do not understand the issue if you do not show the starting data.

To generate a "big" figure such as the one you showed, you produce independent figures for each gene in ggplot2 and then use packages such as cowplot or patchwork to arrange them in a panel. I suggest reading documentation on those.

Log in to answer this question.