-
Notifications
You must be signed in to change notification settings - Fork 11
/
ma_plot.R
208 lines (183 loc) · 6.39 KB
/
ma_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#' @title MversusA plot for visualizing differentially expressed genes.
#' @description MversusA plot for visualizing differentially expressed genes.
#' @author benben-miao
#'
#' @return Plot: MversusA plot for visualizing differentially expressed genes.
#' @param data Dataframe: differentially expressed genes (DEGs) stats 2 (1st-col: Gene, 2nd-col: baseMean, 3rd-col: Log2FoldChange, 4th-col: FDR).
#' @param foldchange Numeric: fold change value. Default: 1.0, min: 0.0, max: null.
#' @param fdr_value Numeric: false discovery rate. Default: 0.05, min: 0.00, max: 1.00.
#' @param point_size Numeric: point size. Default: 1.0, min: 0.0, max: null.
#' @param color_up Character: up-regulated genes color (color name or hex value). Default: "#FF0000".
#' @param color_down Character: down-regulated genes color (color name or hex value). Default: "#008800".
#' @param color_alpha Numeric: point color alpha. Default: 0.50, min: 0.00, max: 1.00.
#' @param top_method Character: top genes select method. Default: "fc" (fold change), options: "padj" (p-adjust), "fc".
#' @param top_num Numeric: top genes number. Default: 20, min: 0, max: null.
#' @param label_size Numeric: label font size. Default: 8.00, min: 0.00, max: null.
#' @param label_box Logical: add box to label. Default: TRUE, options: TRUE, FALSE.
#' @param title Character: plot title. Default: "CT-vs-Trait1".
#' @param xlab Character: x label. Default: "Log2 mean expression".
#' @param ylab Character: y label. Default: "Log2 fold change".
#' @param ggTheme Character: ggplot2 themes. Default: "theme_light", options: "theme_default", "theme_bw", "theme_gray", "theme_light", "theme_linedraw", "theme_dark", "theme_minimal", "theme_classic", "theme_void"
#'
#' @import ggplot2
#' @import ggsci
#' @importFrom ggpubr ggmaplot
#' @export
#'
#' @examples
#' # 1. Library TOmicsVis package
#' library(TOmicsVis)
#'
#' # 2. Use example dataset
#' data(degs_stats2)
#' head(degs_stats2)
#'
#' # 3. Default parameters
#' ma_plot(degs_stats2)
#'
#' # 4. Set color_up = "#FF8800"
#' ma_plot(degs_stats2, color_up = "#FF8800")
#'
#' # 5. Set top_num = 10
#' ma_plot(degs_stats2, top_num = 10)
#'
ma_plot <- function(data,
foldchange = 1.0,
fdr_value = 0.05,
point_size = 3.0,
color_up = "#FF0000",
color_down = "#008800",
color_alpha = 0.50,
top_method = "fc",
top_num = 20,
label_size = 8.00,
label_box = TRUE,
title = "CT-vs-LT12",
xlab = "Log2 mean expression",
ylab = "Log2 fold change",
ggTheme = "theme_light"
){
# -> 2. Data Operation
# data(diff_express)
# # diff_express <- diff_express[diff_express$detection_call == 1,]
# write.table(diff_express, file = "MversusA.txt", quote = F, sep = "\t", row.names = T)
# <- 2. Data Operation
# -> 3. Plot Parameters
# fonts <- "Times"
# ChoiceBox: "Times", "Palatino", "Bookman", "Courier", "Helvetica", "URWGothic", "NimbusMon", "NimbusSan"
# ggTheme <- "theme_minimal"
# ChoiceBox: "theme_default", "theme_bw", "theme_gray", "theme_light", "theme_linedraw", "theme_dark", "theme_minimal", "theme_classic", "theme_void"
if (ggTheme == "theme_default") {
gg_theme <- theme()
} else if (ggTheme == "theme_bw") {
gg_theme <- theme_bw()
} else if (ggTheme == "theme_gray") {
gg_theme <- theme_gray()
} else if (ggTheme == "theme_light") {
gg_theme <- theme_light()
} else if (ggTheme == "theme_linedraw") {
gg_theme <- theme_linedraw()
} else if (ggTheme == "theme_dark") {
gg_theme <- theme_dark()
} else if (ggTheme == "theme_minimal") {
gg_theme <- theme_minimal()
} else if (ggTheme == "theme_classic") {
gg_theme <- theme_classic()
} else if (ggTheme == "theme_void") {
gg_theme <- theme_void()
} else if (ggTheme == "theme_test") {
gg_theme <- theme_test()
}
# title <- "Group1 -versus- Group2"
# TextField
# xlab <- "Log2 mean expression"
# ylab <- "Log2 fold change"
# =====
# fdr_value <- 0.05
# Slider: 0.05, 0.00, 1.00, 0.01
# foldchange <- 2.00
# Slider: 2.00, 0.00, 10.00, 0.01
# point_size <- 0.50
# Slider: 0.50, 0.00, 10.00, 0.01
# label_size <- 8.00
# Slider: 8.00, 0.00, 50.00, 1.00
# labelBox <- "LabelBox_Show"
# # ChoiceBox: "LabelBox_Show", "LabelBox_Hidden"
# if (labelBox == "LabelBox_Show") {
# label_box <- TRUE
# } else if (labelBox == "LabelBox_Hidden") {
# label_box <- FALSE
# }
# color_up <- "#FF0000"
# ColorPicker:
# color_down <- "#008800"
# ColorPicker:
# top_num <- 20
# Slider: 20, 0, 100, 1
# top_method <- "fc"
# ChoiceBox: "padj", "fc"
# =====
plotTitleFace <- "bold"
# ChoiceBox: "plain", "italic", "bold", "bold.italic"
plotTitleSize <- 18
# Slider: 18, 0, 50, 1
plotTitleHjust <- 0.5
# Slider: 0.5, 0.0, 1.0, 0.1
axisTitleFace <- "plain"
# ChoiceBox: "plain", "italic", "bold", "bold.italic"
axisTitleSize <- 16
# Slider: 16, 0, 50, 1
axisTextSize <- 10
# Slider: 10, 0, 50, 1
legendTitleSize <- 12
# Slider: 12, 0, 50, 1
legendPosition <- "right"
# ChoiceBox: "none", "left", "right", "bottom", "top"
legendDirection <- "vertical"
# ChoiceBox: "horizontal", "vertical"
# <- 3. Plot Parameters
# # -> 4. Plot
p <- ggpubr::ggmaplot(data,
fdr = fdr_value,
fc = foldchange,
genenames = as.vector(data[[1]]),
detection_call = NULL,
size = point_size,
alpha = color_alpha,
seed = 123,
font.label = c(label_size, "bold", "black"),
label.rectangle = label_box,
palette = c(color_up, color_down, "#AAAAAA"),
top = top_num,
select.top.method = top_method, # "padj", "fc"
label.select = NULL,
main = title,
xlab = xlab,
ylab = ylab
) +
# geom_text_repel(max.overlaps = Inf) +
gg_theme +
theme(plot.title = element_text(face = plotTitleFace,
# "plain", "italic", "bold", "bold.italic"
size = plotTitleSize,
hjust = plotTitleHjust
),
axis.title = element_text(face = axisTitleFace,
# "plain", "italic", "bold", "bold.italic"
size = axisTitleSize
),
axis.text = element_text(face = "plain",
size = axisTextSize
),
legend.title = element_text(face = "plain",
size = legendTitleSize
),
legend.position = legendPosition,
# "none", "left", "right", "bottom", "top"
legend.direction = legendDirection
# "horizontal" or "vertical"
)
# # <- 4. Plot
return(p)
invisible()
}