|
a |
|
b/Functions/plot.violin.R |
|
|
1 |
#Draw the expression of features in each cluster |
|
|
2 |
|
|
|
3 |
#'@param data Generally requires the expression matrix after log, behavioral genes, listed as samples |
|
|
4 |
#'@param features character vector, |
|
|
5 |
#'@param clusters The sample classification situation is required to be a vector, that is, names (clusters) correspond to the column names of the expression matrix, and you need to arrange the display order yourself |
|
|
6 |
|
|
|
7 |
plot.violin <- function(data, features, clusters, clusters.levels = NULL, xlab = NULL, ylab = NULL, title = NULL, normalizedMethod = "min-max"){ |
|
|
8 |
|
|
|
9 |
# Corresponding gene expression |
|
|
10 |
index <- match(features, rownames(data)) |
|
|
11 |
data <- data[na.omit(index),] |
|
|
12 |
if(normalizedMethod == "min-max"){ |
|
|
13 |
data <- decostand(data, "range", 1) |
|
|
14 |
} |
|
|
15 |
# Corresponding cell classification |
|
|
16 |
index <- match(names(clusters), colnames(data)) |
|
|
17 |
data <- data[, index] |
|
|
18 |
data <- as.data.frame(data) |
|
|
19 |
data$gene <- rownames(data) |
|
|
20 |
|
|
|
21 |
require(tidyr) |
|
|
22 |
data.reshape <- gather(data, sample, expression, colnames(data)[-length(colnames(data))]) |
|
|
23 |
data.reshape$cluster <- rep(clusters, each = nrow(data)) |
|
|
24 |
if(!is.null(clusters.levels)){ |
|
|
25 |
data.reshape$cluster <- factor(data.reshape$cluster, levels = clusters.levels) |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
require(ggpubr) |
|
|
29 |
p <- ggviolin(data.reshape, x = "cluster", y = "expression", fill = "cluster", color = "cluster", add = "mean", add.params = list(color = "black", size = 0.01), xlab = xlab, ylab = ylab, title = title) |
|
|
30 |
p <- p + facet_grid(gene ~ ., scales="free_y") + theme(legend.position="none") |
|
|
31 |
return(p) |
|
|
32 |
} |