Let us first define some values useful to design colour scales:
range.exprs <- range(assay(sce.endo, "logcounts"))
col.treatment <- RColorBrewer::brewer.pal(9, "Paired")[c(9,3,4,1,2)]
names(col.treatment) <- gsub("_","\n",levels(sce.endo$TreatmentLabel))
Let us then define a few convenience functions:
geneNameToID <- function(x){
geneId <- subset(rowData(sce.endo), gene_name == x, "gene_id", drop = TRUE)
if (length(geneId) > 1){
warning("Multiple IDs found. Use ID instead")
return(data.frame(
gene_name = x,
gene_id = geneId
))
} else if (length(geneId) == 0){
stop("gene_name not found")
}
return(geneId)
}
geneDataByName <- function(x){
geneId <- geneNameToID(x)
ggData <- data.frame(
gene = assay(sce.endo, "logcounts")[geneId,],
colData(sce.endo)[,c("Time","Infection","Status","TreatmentLabel")]
)
return(ggData)
}
plotByNameFacetTime <- function(x){
geneId <- geneNameToID(x)
ggData <- geneDataByName(x)
# return(ggData)
ggplot(ggData, aes(TreatmentLabel, gene)) +
facet_grid(Time~.) +
geom_violin(
aes(colour = TreatmentLabel, fill = TreatmentLabel),
alpha = 0.2) +
geom_jitter(
aes(colour = TreatmentLabel), height = 0, width = 0.2,
alpha = 0.5, size = 0.4) +
scale_color_manual(values = col.treatment) +
scale_fill_manual(values = col.treatment) +
labs(
title = sprintf("%s", x),
subtitle = sprintf("%s", geneId),
x = "Treatment",
y = "log-counts",
colour = "Treatment",
fill = "Treatment") +
scale_y_continuous(limits = range.exprs) +
theme_bw() +
theme(
legend.key.height = unit(1.75, "lines")
)
}
plotByNameSubset <- function(
x,
time = c("2h","4h","6h"),
infection = c("Mock","STM-LT2","STM-D23580"),
status = c("Uninfected","Exposed","Infected")
){
geneId <- geneNameToID(x)
ggData <- geneDataByName(x)
ggData <- subset(
ggData,
Time %in% time & Infection %in% infection & Status %in% status)
# return(ggData)
ggplot(ggData, aes(TreatmentLabel, gene)) +
geom_violin(
aes(colour = TreatmentLabel, fill = TreatmentLabel), scale = "width",
alpha = 0.4) +
geom_jitter(
aes(colour = TreatmentLabel), height = 0, width = 0.2,
alpha = 0.5, size = 0.4) +
scale_color_manual(values = col.treatment) +
scale_fill_manual(values = col.treatment) +
labs(
title = sprintf("%s (%s)", x, time),
subtitle = sprintf("%s", geneId),
x = "Treatment",
y = "log-counts",
colour = "Treatment",
fill = "Treatment") +
scale_y_continuous(limits = range.exprs) +
theme_bw() +
theme(
legend.key.height = unit(1.75, "lines"),
axis.text.y = element_text(size = rel(1.5)),
panel.grid.minor = element_blank()
)
}
The above convenience function make it straightforward to produce a figure for any gene of interest:















































