|
a |
|
b/R/VolcanoPlot.R |
|
|
1 |
#' @title Volcano Plot |
|
|
2 |
#' @description Plotting differentially expressed genes (DEGs) in a particular |
|
|
3 |
#' cluster. Volcano plots are used to readily show the DEGs by plotting |
|
|
4 |
#' significance versus fold-change on the y and x axes, respectively. |
|
|
5 |
#' @param object A data frame showing the differentially expressed genes (DEGs) |
|
|
6 |
#' in a particular cluster |
|
|
7 |
#' @param value A numeric value of the false discovery rate. Default is 0.05.. |
|
|
8 |
#' Default is 0.05 |
|
|
9 |
#' @param fc A numeric value of the fold change. Default is 0.5. |
|
|
10 |
#' @param FS A numeric value of the font size. Default is 0.4. |
|
|
11 |
#' @param name A string vector showing the name to be used on the plot title |
|
|
12 |
#' @importFrom graphics title |
|
|
13 |
#' @importFrom utils write.csv |
|
|
14 |
#' @return A volcano plot |
|
|
15 |
#' @export |
|
|
16 |
VolcanoPlot <- function(object, value = 0.05, name = NULL, fc = 0.5, FS = .4) { |
|
|
17 |
if (length(object[1, ]) > 8) { |
|
|
18 |
object <- object[, -1] |
|
|
19 |
} |
|
|
20 |
NO0 <- object[, 8] |
|
|
21 |
NO0 <- NO0[which(NO0 != 0)] |
|
|
22 |
w <- which.min(NO0) |
|
|
23 |
adjV <- NO0[w] / 100 |
|
|
24 |
object[, 8] <- ifelse( |
|
|
25 |
object[, 8] == 0 & length(adjV) > 0, adjV, object[, 8] |
|
|
26 |
) |
|
|
27 |
if (all(object[, 8] == 0)) { |
|
|
28 |
message("All q-values are 0. Adjusting") |
|
|
29 |
object[, 8] <- object[, 8] + 1e-10 |
|
|
30 |
} |
|
|
31 |
with( |
|
|
32 |
object, |
|
|
33 |
plot( |
|
|
34 |
abs(object[, 7]), |
|
|
35 |
-log10(object[, 8]), |
|
|
36 |
pch = 20, |
|
|
37 |
cex = 2, |
|
|
38 |
las = 1, |
|
|
39 |
xlab = "log2 Fold Change", |
|
|
40 |
ylab = "-log10 FDR", |
|
|
41 |
sub = paste("Volcano plot", name), |
|
|
42 |
font.sub = 4, |
|
|
43 |
col.sub = "black" |
|
|
44 |
) |
|
|
45 |
) |
|
|
46 |
FC <- subset(object, abs(object[, 7]) > fc) # Fold Change |
|
|
47 |
sigFC <- subset( |
|
|
48 |
object, object[, 8] < value & abs(object[, 7]) > fc |
|
|
49 |
) # Significant genes |
|
|
50 |
with(FC, points( |
|
|
51 |
abs(FC[, 7]), |
|
|
52 |
-log10(FC[, 8]), |
|
|
53 |
pch = 20, |
|
|
54 |
cex = 2, |
|
|
55 |
col = "red" |
|
|
56 |
)) |
|
|
57 |
with(sigFC, points( |
|
|
58 |
abs(sigFC[, 7]), |
|
|
59 |
-log10(sigFC[, 8]), |
|
|
60 |
pch = 20, |
|
|
61 |
cex = 2, |
|
|
62 |
col = "blue" |
|
|
63 |
)) |
|
|
64 |
add_legend( |
|
|
65 |
"topleft", |
|
|
66 |
legend = c( |
|
|
67 |
paste0("DEGs (FC < ", fc, " - FDR> ", value, ") "), |
|
|
68 |
paste0("DEGs (FC > ", fc, " - FDR> ", value, ")"), |
|
|
69 |
paste0("DEGs (FC > ", fc, " - FDR< ", value, ") ") |
|
|
70 |
), |
|
|
71 |
pch = 20, |
|
|
72 |
col = c("black", "red", "blue"), |
|
|
73 |
horiz = TRUE, |
|
|
74 |
bty = "n", |
|
|
75 |
cex = FS |
|
|
76 |
) |
|
|
77 |
} |