-
Notifications
You must be signed in to change notification settings - Fork 0
/
d__plot.network.R
executable file
·163 lines (127 loc) · 5.33 KB
/
d__plot.network.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
#!/home1/wim/R-3.2.3/bin/Rscript --no-save --no-restore
library( 'reshape2' )
library( 'ggplot2' )
library( 'dplyr' )
#################################
# FUNCTIONS
#################################
# ggplot function
number_ticks <- function( n ) { function( limits ) pretty( limits, n ) }
###
# Plot traces.
##
get.plot.eeg <- function( df )
{
df$time <- 1:nrow( df ) / sf
plot.eeg <- ggplot( data = melt( df, id = 'time' ), aes( x = time, color = variable, group = variable, y = value ) ) +
geom_line() +
facet_wrap( ~variable, scale = 'free_y', ncol = 2 ) +
theme_minimal( base_size = 12 ) + theme( legend.position = 'none' ) + xlab( "Time (s)" ) +
scale_x_continuous( breaks = number_ticks( 25 ) ) +
scale_y_continuous( breaks = number_ticks( 6 ) )
return( plot.eeg )
}
###
# 95% confidence interval for the mean.
# https://www.r-bloggers.com/standard-deviation-vs-standard-error/
##
ci.dev <- function( x )
{
#computation of the standard error of the mean
sem <- sd( x, na.rm = TRUE ) / sqrt( length( x ) )
#95% confidence intervals of the mean
return( 2 * sem )
}
###
# get network data
##
get.network.data <- function( indir, metadir )
{
# get meta data
head( meta <- read.csv( paste0( metadir, '/', 'demographics.csv' ), row.names = 1 ) )
# keep relevant columns only
head( meta <- meta[, c( "subject.id", "group", "deafness", "asl_duration", "sex", "age" ) ] )
head( df <- read.csv( paste0( indir, '/', 'all.network.csv' ), row.names = 1 ) )
# merge with meta-data
head( mdata <- merge( df, meta ) )
# rename bands
levels( mdata$bands ) <- c( 'delta', 'theta', 'alpha', 'beta', 'gamma' )
# only keep deaf and control
mdata <- mdata[ mdata$group %in% c( 'control', 'deaf' ), ]
mdata$group <- as.factor( as.character( mdata$group ) )
# return data
return( mdata )
}
#######################
# END FUNCTIONS
#######################
# to be sure to replicate stuff (like mst calculation).
set.seed( 123 )
# create output directory
metadir <- 'demographics'
indir <- 'merged_fc_epochs_nigeria'
outdir <- 'plot_network_nigeria'
dir.create( outdir, showWarnings = FALSE )
# get network data
df <- get.network.data( indir, metadir )
# write to file
levels( df$deafness ) <- c( "04. unknown", "01. birth", "03. post-lingual", "02. pre-lingual" )
write.csv( df, file = paste0( outdir, '/data.csv' ) )
metrics <- c( "g.mst.strength.max", "g.mst.strength.mean", "g.mst.degree.max", "g.mst.bc.max", "g.mst.bc.median", "g.mst.cc.max",
"g.mst.cc.median", "g.mst.leaf", "g.mst.diameter", "g.mst.ecc", "g.mst.radius", "g.mst.Th", "g.mst.kappa" )
# average across epochs to get single value for condition A and single value for condition B, per subject
for( metric in metrics )
{
print( metric )
df$tmp <- NA
df[ , 'tmp' ] <- df[, metric ]
s <- NULL
s <- as.data.frame( dplyr::summarise( grouped <- dplyr::group_by( df, group, Eyes, bands ), mean = mean( tmp ), ci.dev = ci.dev( tmp ) ) )
# plot
dodge <- position_dodge( 1 )
p <- ggplot( s, aes( x = Eyes, y = mean, ymin = mean - ci.dev, ymax = mean + ci.dev, group = group, fill = group, colour = group ) ) +
geom_line( alpha = 0.5, position = dodge, size = 1 ) +
geom_errorbar( colour = 'gray50', position = dodge, width = 0.5 ) +
geom_point( alpha = 1, stat = 'identity', position = dodge, size = 3 ) +
facet_wrap( ~bands, nrow = 1 ) +
scale_y_continuous( breaks = number_ticks( 8 ) ) +
scale_fill_manual( values = c("#1f78b4", "#E69F00", "gray50" ) ) +
scale_color_manual( values = c("#1f78b4", "#E69F00", "gray50" ) ) +
xlab( "Eyes condition" ) +
ylab( paste0( "Network metrics [", metric, "]" ) ) +
theme_classic( base_size = 14 ) +
theme( legend.position = 'top', axis.title = element_text( face = "bold" ) )
# save to file
ggsave( file = paste0( outdir, '/network-metrics__', metric, '.png' ), plot = p, dpi = 200, height = 6, width = 8 )
}
## for type of deafness
for( metric in metrics )
{
print( metric )
df$tmp <- NA
df[ , 'tmp' ] <- df[, metric ]
s <- NULL
s <- as.data.frame( dplyr::summarise( grouped <- dplyr::group_by( df, group, deafness, Eyes, bands ), mean = mean( tmp ), ci.dev = ci.dev( tmp ) ) )
s$deafness <- ordered( as.character( s$deafness ) )
s <- s[ s$deafness != "04. unknown", ]
levels( s$deafness ) <- c( "congenital", "pre-lingual", "post-lingual", "unknown" )
metric.label <- paste0( "Network metrics [", metric, "]" )
if( metric == 'g.mst.strength.mean' )
metric.label <- 'Mean strength'
# plot
dodge <- position_dodge( 1 )
p <- ggplot( s, aes( x = Eyes, y = mean, ymin = mean - ci.dev, ymax = mean + ci.dev, group = deafness, fill = deafness, colour = deafness ) ) +
geom_errorbar( colour = 'gray90', position = dodge, width = 0.5 ) +
geom_line( alpha = 0.5, position = dodge, size = 1 ) +
geom_point( alpha = 1, stat = 'identity', position = dodge, size = 3 ) +
facet_wrap( ~bands, nrow = 1 ) +
scale_y_continuous( breaks = number_ticks( 8 ) ) +
scale_fill_manual( values = c( "red", "#E69F00", "#1f78b4", "gray50" ) ) +
scale_color_manual( values = c("red", "#E69F00", "#1f78b4", "gray50" ) ) +
xlab( "Eyes condition" ) +
ylab( metric.label ) +
theme_classic( base_size = 14 ) +
theme( legend.position = 'top', axis.title = element_text( face = "bold" ) )
# save to file
ggsave( file = paste0( outdir, '/deafness___network-metrics__', metric, '.png' ), plot = p, dpi = 200, height = 6, width = 8 )
}