-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.brainperm.R
143 lines (108 loc) · 3.86 KB
/
plot.brainperm.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
#' Plot a graph of the channels for 1 sample/time-point
#'
#' @description Plot the graphs of the channels for several samples. Red indicates significant clusters, grey non-significant and white represent below the threshold.
#'
#' @param x brainperm object with a clustermass test.
#' @param effect an integer specifying which effect to plot.
#' @param samples a vector of integers specifying the samples to plots.
#' @param ... other arguments including: \code{alpha}, the type I error rate, \code{alternative}, the alternative hypothesis and \code{rad} the radius of the circles.
#'
#' @return a plot of a graph of the channels.
#'
#' @importFrom graphics par plot text
#' @importFrom igraph get.edgelist
#' @family plotting functions
#' @export
plot.brainperm <- function(x, effect = 1, samples,...){
# save parameters
par0 <- par()
dotargs = list(...)
dotargs_par <- dotargs[names(dotargs) %in% names(par())]
if(is.null(dotargs$alpha)){dotargs$alpha = 0.05}
###
if (is.null(dotargs$alternative)) {
dotargs$alternative = "two.sided"
}
if(x$test == "fisher"){
dotargs$alternative = "two.sided"
}
dotargs$alternative <- match.arg(dotargs$alternative,choices = c("two.sided","greater","less"),several.ok = F)
switch(dotargs$alternative,
"two.sided" = {multiple_comparison = x$multiple_comparison},
"greater" = {multiple_comparison = x$multiple_comparison_greater},
"less" = {multiple_comparison = x$multiple_comparison_less})
##
if (is.null(dotargs_par$mar)) {
dotargs_par$mar = c(0, 0, 2, 0)
}
if (is.null(dotargs_par$oma)) {
dotargs_par$oma = c(4, 4, 4, 1)
}
par(dotargs_par)
#Vertex data
graph <- x$graph
df <- data.frame(vertex_attr(graph),stringsAsFactors = F)
df <- with(df,xyz2polar(name,x,y,z))
#radius of channels
if(is.null(dotargs$rad)){
if(length(df$x)!=1){
rad <- min(dist(cbind(df$x,df$y)),na.rm = T)*(1/4)} else{
rad <-0.4
}}else{
rad = dotargs$rad
}
#Edge data
df_edge <- data.frame(get.edgelist(graph),stringsAsFactors = F)
colnames(df_edge)= c("from","to")
df_edge <- left_join(df_edge,df,by = c("from"="name") )
colnames(df_edge)[3:4] <- c("x.from","y.from")
df_edge <- left_join(df_edge,df,by = c("to"="name") )
colnames(df_edge)[5:6] <- c("x.to","y.to")
#plot dimension
if(length(df$x)==1){
xlim <- c(df$x-0.5,df$x+0.5)
}else{
xlim <- range(df$x)
}
if(length(df$y)==1){
ylim <- c(df$y-0.5,df$y+0.5)
}else{
ylim <- range(df$y)
}
xlim <- c(xlim[1]-rad,xlim[2]+rad)
ylim <- c(ylim[1]-rad,ylim[2]+rad)
#mfrow args
mfrow <- floor(sqrt(length(samples)))
mfrow <- c(mfrow,ceiling(length(samples)/mfrow))
if (is.null(dotargs_par$mfrow)) {
dotargs_par$mfrow <- mfrow
}
title <- names(multiple_comparison)[effect]
par(dotargs_par)
for(sample_id in 1:length(samples)){
main <- paste0("sample: ",samples[sample_id])
plot(0,type="n",xlim = xlim,ylim = ylim, bty ="n",
xaxt = "n",yaxt = "n",xlab = "",ylab = "", main = main)
with(df_edge,segments(x.from,y.from,x.to,y.to))
df_sampi <- left_join(df,subset(multiple_comparison[[effect]][[x$multcomp]]$data,
sample==(samples[sample_id])), by = c("name" = "channel"))
if(x$test!="clustermass"){df_sampi$cluster_id = as.numeric(df_sampi$pvalue<dotargs$alpha)}
u_cl <- unique(df_sampi$cluster_id)
for(cli in u_cl){
df_cli = df_sampi[df_sampi$cluster_id == cli,,drop=F]
if(cli==0){
col = "white"
}else if(df_cli$pvalue[1]>=dotargs$alpha){
col = "grey"
}else if(df_cli$pvalue[1]<dotargs$alpha){
col = "red"}
for(i in 1:nrow(df_cli)){
draw_circle(df_cli$x[i],df_cli$y[i],radius = rad,col =col)
}
}
text(labels = df$name,x=df$x,y=df$y)
}
title(title, outer = T, cex = 2)
par0 <- par0[!names(par0) %in% c("cin", "cra", "csi", "cxy", "din", "page")]
par(par0)
}