-
Notifications
You must be signed in to change notification settings - Fork 11
/
umap_plot.R
288 lines (266 loc) · 9.64 KB
/
umap_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#' @title UMAP plot for analyzing and visualizing UMAP algorithm.
#' @description UMAP plot for analyzing and visualizing UMAP algorithm.
#' @author benben-miao
#'
#' @return Plot: UMAP plot for analyzing and visualizing UMAP algorithm.
#' @param sample_gene Dataframe: gene expression dataframe (1st-col: Transcripts or Genes, 2nd-col~: Samples).
#' @param group_sample Dataframe: Samples and groups for gene expression (1st-col: Samples, 2nd-col: Groups).
#' @param seed Numeric: set seed for robust result. Default: 1.
#' @param multi_shape Logical: groups as shapes. Default: FALSE, options: TRUE, FALSE.
#' @param point_size Numeric: point size. Default: 5, min: 0, max: null.
#' @param point_alpha Numeric: point color alpha. Default: 0.80, min: 0.00, max: 1.00.
#' @param text_size Numeric: text size. Default: 5, min: 0 (hind), max: null.
#' @param text_alpha Numeric: text alpha. Default: 0.80, min: 0.00, max: 1.00.
#' @param fill_alpha Numeric: ellipse alpha. Default: 0.30, min: 0.00, max: 1.00.
#' @param border_alpha Numeric: ellipse border color alpha. Default: 0.10, min: 0.00, max: 1.00.
#' @param sci_fill_color Character: ggsci color pallet. Default: "Sci_AAAS", options: "Sci_AAAS", "Sci_NPG", "Sci_Simpsons", "Sci_JAMA", "Sci_GSEA", "Sci_Lancet", "Sci_Futurama", "Sci_JCO", "Sci_NEJM", "Sci_IGV", "Sci_UCSC", "Sci_D3", "Sci_Material".
#' @param legend_pos Character: legend position. Default: "right", options: "none", "left", "right", "bottom", "top".
#' @param legend_dir Character: legend direction. Default: "vertical", options: "horizontal", "vertical".
#' @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 umap umap
#' @importFrom vegan anosim
#' @importFrom grDevices rgb
#' @importFrom ggforce geom_mark_ellipse
#' @export
#'
#' @examples
#' # 1. Library TOmicsVis package
#' library(TOmicsVis)
#'
#' # 2. Use example dataset
#' data(gene_expression)
#' head(gene_expression)
#'
#' data(samples_groups)
#' head(samples_groups)
#'
#' # 3. Default parameters
#' umap_plot(gene_expression, samples_groups)
#'
#' # 4. Set sci_fill_color = "Sci_Simpsons", seed = 6
#' umap_plot(gene_expression, samples_groups, sci_fill_color = "Sci_Simpsons", seed = 6)
#'
#' # 5. Set fill_alpha = 0.10
#' umap_plot(gene_expression, samples_groups, fill_alpha = 0.10)
#'
umap_plot <- function(sample_gene,
group_sample,
seed = 1,
multi_shape = TRUE,
point_size = 5,
point_alpha = 1.00,
text_size = 5,
text_alpha = 0.80,
fill_alpha = 0.00,
border_alpha = 0.00,
sci_fill_color = "Sci_AAAS",
legend_pos = "right",
legend_dir = "vertical",
ggTheme = "theme_light"
){
# -> 2. NA and Duplicated
sample_gene <- as.data.frame(sample_gene)
rownames(sample_gene) <- sample_gene[,1]
sample_gene <- sample_gene[,-1]
sample_gene <- sample_gene[rowSums(sample_gene > 0) > 0, ]
t_sample_gene <- t(sample_gene)
groups <- group_sample[,2]
umap_ano <- vegan::anosim(x = t_sample_gene,
grouping = groups)
umap_p <- umap_ano$signif
umap_r <- round(umap_ano$statistic,3)
set.seed(seed)
umap_res <- umap(t_sample_gene)
umap_out <- as.data.frame(umap_res$layout[,c(1,2)])
# write.table(umap_out,
# file = "Results.txt",
# append = FALSE,
# sep = "\t",
# quote = FALSE,
# na = "NA"
# )
colnames(umap_out) <- c("UMAP1","UMAP2")
# <- 2. NA and Duplicated
# -> 3. Plot parameters
# fonts <- "Times"
# ChoiceBox: "Times", "Palatino", "Bookman", "Courier", "Helvetica", "URWGothic", "NimbusMon", "NimbusSan"
# ggTheme <- "theme_light"
# 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()
}
# point_size <- 3
# slide: 5, 0, 0.1, 20
# point_alpha <- 0.8
# slide: 0.8, 0, 0.1, 1
# text_size <- 2
# slide: 6, 0, 0.1, 20
# text_alpha <- 0.8
# slide: 0.8, 0, 0.1, 1
# ellipse_alpha <- 0.3
# slide: 0.3, 0, 0.1, 1
# ci_level <- 0.95
# slide: 0.95, 0, 0.01, 1
sci_color_alpha <- 1.00
# sci_fill_color <- "Sci_NPG"
# ChoiceBox: "Sci_AAAS", "Sci_NPG", "Sci_Simpsons", "Sci_JAMA", "Sci_GSEA", "Sci_Lancet", "Sci_Futurama", "Sci_JCO", "Sci_NEJM", "Sci_IGV", "Sci_UCSC", "Sci_D3", "Sci_Material"
if (sci_fill_color == "Default") {
sci_color <- NULL
} else if (sci_fill_color == "Sci_AAAS") {
sci_color <- scale_color_aaas(alpha = sci_color_alpha)
# Science and Science Translational Medicine:
} else if (sci_fill_color == "Sci_NPG") {
sci_color <- scale_color_npg(alpha = sci_color_alpha)
} else if (sci_fill_color == "Sci_Simpsons") {
sci_color <- scale_color_simpsons(alpha = sci_color_alpha)
# The Simpsons
} else if (sci_fill_color == "Sci_JAMA") {
sci_color <- scale_color_jama(alpha = sci_color_alpha)
# The Journal of the American Medical Association
} else if (sci_fill_color == "Sci_Lancet") {
sci_color <- scale_color_lancet(alpha = sci_color_alpha)
# Lancet Oncology
} else if (sci_fill_color == "Sci_Futurama") {
sci_color <- scale_color_futurama(alpha = sci_color_alpha)
# Futurama
} else if (sci_fill_color == "Sci_JCO") {
sci_color <- scale_color_jco(alpha = sci_color_alpha)
# Journal of Clinical Oncology:
} else if (sci_fill_color == "Sci_NEJM") {
sci_color <- scale_color_nejm(alpha = sci_color_alpha)
# The New England Journal of Medicine
} else if (sci_fill_color == "Sci_IGV") {
sci_color <- scale_color_igv(alpha = sci_color_alpha)
# Integrative Genomics Viewer (IGV)
} else if (sci_fill_color == "Sci_UCSC") {
sci_color <- scale_color_ucscgb(alpha = sci_color_alpha)
# UCSC Genome Browser chromosome sci_color
} else if (sci_fill_color == "Sci_D3") {
sci_color <- scale_color_d3(alpha = sci_color_alpha)
# D3.JS
} else if (sci_fill_color == "Sci_Material") {
sci_color <- scale_color_material(alpha = sci_color_alpha)
# The Material Design color palettes
}
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 <- 14
# Slider: 16, 0, 50, 1
axisTextSize <- 10
# Slider: 10, 0, 50, 1
legendTitleSize <- 12
# Slider: 12, 0, 50, 1
# legend_pos <- "right"
# ChoiceBox: "none", "left", "right", "bottom", "top"
# legend_dir <- "vertical"
# ChoiceBox: "horizontal", "vertical"
# <- 3. Plot parameters
# # -> 4. Plot
labels <- row.names(t_sample_gene)
if (multi_shape) {
p <- ggplot(umap_out,
aes_string(x = "UMAP1",
y = "UMAP2",
color = "groups",
shape = "groups",
label = "labels")
)
}else {
p <- ggplot(umap_out,
aes_string(x = "UMAP1",
y = "UMAP2",
color = "groups",
# shape = "groups",
label = "labels")
)
}
p <- p +
geom_point(size = point_size,
show.legend = TRUE,
alpha = point_alpha) +
geom_text(size = text_size,
alpha = text_alpha,
show.legend = FALSE,
hjust = -0.2,
vjust = 0.4) +
xlab("UMAP1") +
ylab("UMAP2") +
geom_mark_ellipse(aes(fill = groups),
label.fontsize = 0,
label.colour = "#ffffff00",
label.fill = "#ffffff00",
con.size = 0,
con.colour = "#ffffff00",
color = rgb(0, 0, 0, border_alpha),
alpha = fill_alpha,
show.legend = TRUE
# level = pca_ellipse_level
) +
annotate("text",
x = min(umap_out$UMAP1) + ((max(umap_out$UMAP1) - min(umap_out$UMAP1)) * 0.01),
y = max(umap_out$UMAP2),
parse = TRUE,
size = 5,
label = paste('R:',umap_r),
colour = "black") +
annotate("text",
x = min(umap_out$UMAP1) + ((max(umap_out$UMAP1) - min(umap_out$UMAP1)) * 0.01),
y = max(umap_out$UMAP2) - ((max(umap_out$UMAP2) - min(umap_out$UMAP2)) * 0.05),
parse = TRUE,
size = 5,
label = paste('P:',umap_p),
colour = "black") +
labs(fill = "Groups", color = "Groups", shape = "Groups") +
sci_color +
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 = legend_pos,
# "none", "left", "right", "bottom", "top"
legend.direction = legend_dir
# "horizontal" or "vertical"
)
# # <- 4. Plot
return(p)
invisible()
}