forked from ctlab/ImmGenOpenSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fig4_radarCharts.R
106 lines (95 loc) · 2.8 KB
/
Fig4_radarCharts.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
library(tidyverse)
load("Data/eyegene.rda")
anno <- read_csv("Data/anno.csv")
df <- eyegene %>% as_tibble()
# Glimpse on annotation
anno %>%
count(value) %>%
arrange(desc(n))
# Calculate per-module statistics
stats <- df %>%
mutate(module = row_number()) %>%
gather('sample', 'value', -module) %>%
group_by(module) %>%
summarize(max = max(value),
min = min(value))
stats
plot_radarchart <- function(long) {
long %>%
spread(module, value) %>%
as.data.frame() %>%
column_to_rownames('sample') %>% {
# bind in following order: max, min, data, mean
rbind(stats$max, # + 1, # max(stats$max)
stats$min, # - 1, # min(stats$min)
# 0,
.,
means)
} %>% {
.[, c(1, ncol(.):2)]
} %>%
fmsb::radarchart(
pty = 32, # point symbol (16 filled circle, 32 nothing)
pcol = line_color,
plwd = line_width,
plty = line_type,
cglcol = 'gray22', # grid color
cglwd = 1, # grid width
cglty = 2, # grid type (1 solid, 2 dashed, 3 dotted)
axistype = 0, # just 0
axislabcol = "black", # labels color
vlcex = 1 # font size magnification for labels
)
}
plot_radarchart_pdf <- function(long, filename) {
pdf(filename, width = 4, height = 4)
plot_radarchart(long)
dev.off()
}
plot_radarchart_png <- function(long, filename) {
png(filename)
plot_radarchart(long)
dev.off()
}
# Choose specific meta-sample
load("Data/annotation_colors.Rda")
names(annotation_colors$metaSample)
for(metasample in names(annotation_colors$metaSample)){
print(metasample)
# Pick specific meta-sample
filtered <- df %>%
select(select_vars(names(df),
anno %>%
filter(value == metasample) %>%
pull(samples)))
# Convert to long format
long <- filtered %>%
# mutate(module = str_c('Mod.', row_number())) %>%
mutate(module = row_number()) %>%
gather('sample', 'value', -module)
# Calculate means
means <- long %>%
group_by(module) %>%
summarize(mean = mean(value)) %>%
pull(mean)
# Number of samples
k <- ncol(filtered) %>% print()
{ # Setup plot line color
color_all <- 'ivory4' # all samples
color_mean <- unname(annotation_colors$metaSample[metasample]) # mean of sample # navy
line_color <- c(rep(color_all, k), color_mean)
}
{ # Setup plot line width
width_all <- 2
width_mean <- 4
line_width <- c(rep(width_all, k), width_mean)
}
{ # Setup plot line type (1 solid, 2 dashed, 3 dotted)
type_all <- 1
type_mean <- 1
line_type <- c(rep(type_all, k), type_mean)
}
# plot_radarchart(long)
plot_radarchart_pdf(long, str_interp('spiderplot-${metasample}.pdf'))
# plot_radarchart_png(long, str_interp('spiderplot-${metasample}.png'))
}