|
a |
|
b/R/network_display.R |
|
|
1 |
#' @title Interactive Network Visualization |
|
|
2 |
#' @description An interactive function to assist in the visualization of the result from INDEED |
|
|
3 |
#' functions non_partial_corr() or patial_corr(). The size and the color of each node can be |
|
|
4 |
#' adjusted by users to represent either the Node_Degree, Activity_Score, Z_Score, or P_Value. |
|
|
5 |
#' The color of the edge is based on the binary value of either 1 corresponding to a positive |
|
|
6 |
#' correlation depicted as green or a negative correlation of -1 depicted as red. Users also |
|
|
7 |
#' have the option of having the width of each edge be proportional to its weight value. The |
|
|
8 |
#' layout of the network can also be customized by choosing from the options: 'nice', 'sphere', |
|
|
9 |
#' 'grid', 'star', and 'circle'. Nodes can be moved and zoomed in on. Each node and edge will |
|
|
10 |
#' display extra information when clicked on. Secondary interactions will be highlighted as |
|
|
11 |
#' well when a node is clicked on. |
|
|
12 |
#' @param result This is the result from calling either non_partial_corr() or partial_corr(). |
|
|
13 |
#' @param nodesize This parameter determines what the size of each node will represent. The options |
|
|
14 |
#' are 'Node_Degree', 'Activity_Score','P_Value' and 'Z_Score'. The title of the resulting |
|
|
15 |
#' network will identify which parameter is selected to represent the node size. The default |
|
|
16 |
#' is Node_Degree. |
|
|
17 |
#' @param nodecolor This parameter determines what color each node will be based on a yellow to |
|
|
18 |
#' blue color gradient. The options are 'Node_Degree', 'Activity_Score', 'P_Value', and ' |
|
|
19 |
#' Z_Score'. A color bar will be created based on which parameter is chosen. The default is |
|
|
20 |
#' Activity_Score. |
|
|
21 |
#' @param edgewidth This is a boolean value to indicate whether the edgewidth should be representative |
|
|
22 |
#' of the weight connection (TRUE) or not (FALSE). The default is FALSE. |
|
|
23 |
#' @param layout Users can choose from a a handful of network visualization templates including: |
|
|
24 |
#' 'nice', 'sphere', 'grid', 'star', and 'circle'. The default is nice. |
|
|
25 |
#' @examples result = non_partial_cor(data = Met_GU, class_label = Met_Group_GU, id = Met_name_GU, |
|
|
26 |
#' method = "pearson", p_val = pvalue_M_GU, permutation = 1000, |
|
|
27 |
#' permutation_thres = 0.05, fdr = FALSE) |
|
|
28 |
#' network_display(result = result, nodesize = 'Node_Degree', nodecolor = 'Activity_Score', |
|
|
29 |
#' edgewidth = FALSE, layout = 'nice') |
|
|
30 |
#' @return An interactive depiction of the network resulting from INDEED functions |
|
|
31 |
#' non_partial_corr() or patial_corr(). |
|
|
32 |
#' @import igraph |
|
|
33 |
#' @import visNetwork |
|
|
34 |
#' @importFrom grDevices topo.colors |
|
|
35 |
#' @export |
|
|
36 |
|
|
|
37 |
network_display <- function(result = NULL, nodesize= 'Node_Degree', nodecolor= 'Activity_Score', |
|
|
38 |
edgewidth= FALSE, layout= 'nice'){ |
|
|
39 |
|
|
|
40 |
nodes <- result$activity_score |
|
|
41 |
links <- result$diff_network |
|
|
42 |
|
|
|
43 |
# Adding Z_Score to dataframe |
|
|
44 |
Z_Score <- abs(qnorm(1 - (nodes$P_value)/2)) # trasfer p-value to z-score |
|
|
45 |
nodes$zscore = Z_Score |
|
|
46 |
|
|
|
47 |
vis.nodes <- data.frame(id= nodes$Node, name= nodes$ID,font.size = 24, pval= nodes$P_value, |
|
|
48 |
ndegree= nodes$Node_Degree, ascore= nodes$Activity_Score, |
|
|
49 |
zscore= nodes$zscore, stringsAsFactors = FALSE) |
|
|
50 |
vis.links <- data.frame(from=links$Node1, to=links$Node2, binary= links$Binary, |
|
|
51 |
weight= links$Weight) |
|
|
52 |
|
|
|
53 |
vis.nodes$shape <- "dot" |
|
|
54 |
vis.nodes$shadow <- TRUE # Nodes will drop shadow |
|
|
55 |
# Information that will be displayed when hovering over a node |
|
|
56 |
vis.nodes$title <- paste0("<p>", paste('ID: ', vis.nodes$name), "<br>","<br>", |
|
|
57 |
paste('Node Degree: ', vis.nodes$ndegree),"<br>", |
|
|
58 |
paste('Activity Score: ', vis.nodes$ascore),"<br>", |
|
|
59 |
paste('P-value: ', vis.nodes$pval),"<br>", |
|
|
60 |
paste('Z-score: ', round(vis.nodes$zscore, digits=3)),"</p>") |
|
|
61 |
|
|
|
62 |
# Setting up the Node Size |
|
|
63 |
if (missing(nodesize)){ |
|
|
64 |
vis.nodes$size <- (rank(-1 * vis.nodes$pval)+1) |
|
|
65 |
nodesize <- 'p-value significance ' |
|
|
66 |
} |
|
|
67 |
else if (nodesize == 'Node_Degree'){ |
|
|
68 |
vis.nodes$size <- ((vis.nodes$ndegree)+1)*5 |
|
|
69 |
nodesize <- 'Node Degree' |
|
|
70 |
} |
|
|
71 |
else if(nodesize == 'Activity_Score'){ |
|
|
72 |
vis.nodes$size <- ((vis.nodes$ascore)+1)*5 |
|
|
73 |
nodesize <- 'Activity Score' |
|
|
74 |
} |
|
|
75 |
else if(nodesize == 'P_Value'){ |
|
|
76 |
vis.nodes$size <- (rank(-1 * vis.nodes$pval)+1) |
|
|
77 |
nodesize <- 'p-value significance' |
|
|
78 |
} |
|
|
79 |
else if(nodesize == 'Z_Score'){ |
|
|
80 |
vis.nodes$size <- ((vis.nodes$zscore)+1)*10 |
|
|
81 |
nodesize <- 'Z-Score' |
|
|
82 |
} else { |
|
|
83 |
vis.nodes$size <- (rank(-1 * vis.nodes$pval)+1) |
|
|
84 |
nodesize <- 'p-value significance' |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
H <- "Higher" |
|
|
88 |
L <- "Lower" |
|
|
89 |
M <- " " |
|
|
90 |
|
|
|
91 |
# Setting Up Node Color |
|
|
92 |
if (missing(nodecolor)){ |
|
|
93 |
vis.nodes<- vis.nodes[order(vis.nodes$ascore, decreasing=TRUE), ] |
|
|
94 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
95 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
96 |
nodecolor <- 'Activity Score' |
|
|
97 |
} |
|
|
98 |
else if (nodecolor == 'Node_Degree'){ |
|
|
99 |
vis.nodes<- vis.nodes[order(vis.nodes$ndegree, decreasing=TRUE), ] |
|
|
100 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$ndegree), alpha=1) |
|
|
101 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$ndegree), alpha=1) |
|
|
102 |
nodecolor <- 'Node Degree' |
|
|
103 |
} |
|
|
104 |
else if(nodecolor == 'Activity_Score'){ |
|
|
105 |
vis.nodes<- vis.nodes[order(vis.nodes$ascore, decreasing=TRUE), ] |
|
|
106 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
107 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
108 |
nodecolor <- 'Activity Score' |
|
|
109 |
} |
|
|
110 |
else if(nodecolor == 'P_Value'){ |
|
|
111 |
vis.nodes<- vis.nodes[order(vis.nodes$pval, decreasing=FALSE), ] |
|
|
112 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$pval), alpha=1) |
|
|
113 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$pval), alpha=1) |
|
|
114 |
nodecolor <- 'p-value' |
|
|
115 |
H <- "Lower" |
|
|
116 |
L <- "Higher" |
|
|
117 |
M <- " " |
|
|
118 |
} |
|
|
119 |
else if(nodecolor == 'Z_Score'){ |
|
|
120 |
vis.nodes<- vis.nodes[order(vis.nodes$zscore, decreasing=TRUE), ] |
|
|
121 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$zscore), alpha=1) |
|
|
122 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$zscore), alpha=1) |
|
|
123 |
nodecolor <- 'Z-Score' |
|
|
124 |
} |
|
|
125 |
else { |
|
|
126 |
vis.nodes<- vis.nodes[order(vis.nodes$ascore, decreasing=TRUE), ] |
|
|
127 |
vis.nodes$color.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
128 |
vis.nodes$color.highlight.background <- topo.colors(length(vis.nodes$ascore), alpha=1) |
|
|
129 |
nodecolor <- 'Activity Score' |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
vis.nodes$borderWidth <- 2 # Node border width |
|
|
133 |
vis.nodes$label <- vis.nodes$name # Node label |
|
|
134 |
vis.nodes$color.highlight.border <- "darkred" |
|
|
135 |
vis.nodes$color.border <- "black" |
|
|
136 |
|
|
|
137 |
# Setting up edge width parameter |
|
|
138 |
if (edgewidth == TRUE ){vis.links$width <- abs(vis.links$weight) * 3 |
|
|
139 |
} else {vis.links$width <- 3} |
|
|
140 |
# Information that will be displayed when hovering over the edge |
|
|
141 |
vis.links$title <- paste0("<p>", paste('Edge Weight: ', |
|
|
142 |
round(abs(vis.links$weight), digits= 3)), "</p>") |
|
|
143 |
vis.links$color[vis.links$binary == 1] <- "green" # line color |
|
|
144 |
vis.links$color[vis.links$binary == -1] <- "red" |
|
|
145 |
vis.links$arrowStrikethrough <- FALSE |
|
|
146 |
vis.links$smooth <- TRUE # should the edges be curved? |
|
|
147 |
vis.links$shadow <- TRUE # edge shadow |
|
|
148 |
|
|
|
149 |
# Setting up layout of network |
|
|
150 |
if (missing(layout)){l <- "layout_nicely"} |
|
|
151 |
else if (layout == 'nice'){l <- "layout_nicely"} |
|
|
152 |
else if (layout == 'sphere'){l <- "layout_on_sphere"} |
|
|
153 |
else if (layout == 'star'){l <- "layout_as_star"} |
|
|
154 |
else if (layout == 'grid'){l <- "layout_on_grid"} |
|
|
155 |
else if (layout == 'circle'){l <- "layout_in_circle" |
|
|
156 |
} else {l <- "layout_nicely"} |
|
|
157 |
|
|
|
158 |
lnodes <- data.frame(label= c(H, M, L), position = "left", shape= c("circle"), |
|
|
159 |
color= c("blue", "green", "yellow")) |
|
|
160 |
ledges <- data.frame(color= c("green", "red"), label= c("Positive Change in Correlation", |
|
|
161 |
"Negative Change in Correlation"), |
|
|
162 |
font.align= "top", arrows= c("NA", "NA"), width= 4) |
|
|
163 |
|
|
|
164 |
net <- visNetwork(vis.nodes, vis.links, width = "100%", height = "800px", main= "INDEED", |
|
|
165 |
submain= paste("Node size represents: ", nodesize)) %>% |
|
|
166 |
visOptions( highlightNearest= TRUE, nodesIdSelection= TRUE) %>% |
|
|
167 |
visIgraphLayout(layout=l) %>% |
|
|
168 |
visInteraction( dragView= TRUE, dragNodes= TRUE, zoomView= TRUE, navigationButtons= FALSE, |
|
|
169 |
hideEdgesOnDrag= FALSE, multiselect = TRUE) %>% |
|
|
170 |
visLegend(addEdges= ledges, addNodes= lnodes, position= "right", useGroups= FALSE, ncol=1, |
|
|
171 |
main= paste("Node color based on ", nodecolor), width= 0.2, zoom = TRUE) |
|
|
172 |
print(net) |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|