Data generation

Data specs:

set.seed(1)

n_cell_types <- 10
n_genes <- 3
n_cells <- 1e3
n_tissue <- 2
range_expression <- c(100:112)
marker_expression <- 130

gene_exp <- list()
for (cell in 1:n_cell_types) {
  for(gene in 1:n_genes) {
    for (tissue in 1:n_tissue) {
      exp_vector <- rnorm(n_cells, sample(range_expression, 1))
      
      if(gene == 1 & (cell == 3 | cell == 9))
        exp_vector <- rnorm(n_cells, marker_expression)

      gene_exp[[paste(cell, gene, tissue)]] <- data.frame(tissue = paste("tissue", toupper(letters[tissue])), 
                                                          cell_type = paste("cell", letters[cell]), 
                                                          gene = paste("gene", gene), 
                                                          expression = exp_vector)
    }
  }
}
gene_exp <- do.call(rbind, gene_exp)

Mock-up 1: simplest case 1 gene, 1 tissue. With jitter.

ggplot(filter(gene_exp, tissue=="tissue A", gene=="gene 1"), aes(x=cell_type, y=expression)) +
  geom_boxplot(outlier.shape = NA, colour='darkred', size=0.8, width=0.5) +
  geom_jitter(alpha=0.1, size = 0.5) + 
  theme_classic()

Mock-up 2: simplest case 1 gene, 1 tissue. With violin.

ggplot(filter(gene_exp, tissue=="tissue A", gene== "gene 1"), aes(x=cell_type, y=expression)) +
  geom_violin(colour="coral1", fill="coral1") + 
  geom_boxplot(outlier.shape = NA, colour='darkred', size=0.6, width=0.5) +
  theme_classic()

Mock-up 3: multiple genes with 1 tissue. With violin.

ggplot(filter(gene_exp, tissue=="tissue A"), aes(x=gene, y=expression, colour=gene)) +
  geom_violin(aes(colour=gene, fill=gene)) + 
  geom_boxplot(aes(colour=gene), outlier.shape = NA, size=0.5) +
  facet_grid(.~cell_type) + 
  theme_bw() +
  theme(axis.text.x = element_blank())

Mock-up 4: multiple genes with multiple tissues. With violin.

ggplot(filter(gene_exp), aes(x=gene, y=expression, colour=gene)) +
  geom_violin(aes(colour=gene, fill=gene)) + 
  geom_boxplot(aes(colour=gene), outlier.shape = NA, size=0.4) +
  facet_grid(tissue~cell_type) + 
  theme_bw() +
  theme(axis.text.x = element_blank())