summaryrefslogtreecommitdiff
path: root/R/networkplot.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/networkplot.R')
-rw-r--r--R/networkplot.R27
1 files changed, 18 insertions, 9 deletions
diff --git a/R/networkplot.R b/R/networkplot.R
index 2401cb1..cbea488 100644
--- a/R/networkplot.R
+++ b/R/networkplot.R
@@ -9,6 +9,8 @@
#' @param R A square numeric matrix with names columns and rows
#' @param title Optional plot title
#' @param groups Optional data.frame mapping nodes to groups
+#' @param group_label Optional string labeling the kind of grouping
+#' @param r_label The string naming the relationship type (default 'Correlation')
#'
#' @return A ggraph object
#'
@@ -27,7 +29,7 @@
#' @import igraph
#'
#' @export
-networkplot <- function(R, groups = NULL, title = NULL){
+networkplot <- function(R, groups = NULL, group_label = NULL, title = NULL, r_label = 'Correlation'){
Rm <- tryCatch({
if(is.matrix(R)){
if(nrow(R) != ncol(R)) stop('R must be square')
@@ -71,6 +73,9 @@ networkplot <- function(R, groups = NULL, title = NULL){
to = colnames(Rm)[idx[,2]],
r = Rm[idx]
)
+ # remove edges where r is zero
+ edges <- edges[edges$r != 0,]
+ edges <- edges[order(abs(edges$r)),]
# groups must be a data.frame with 2 columns 'node' and 'group'
if(!is.null(groups)){
@@ -133,7 +138,7 @@ networkplot <- function(R, groups = NULL, title = NULL){
ggraph::geom_conn_bundle(
data = con,
ggplot2::aes(
- edge_alpha = abs(.data$value)^2,
+ edge_alpha = abs(.data$value),
edge_colour = .data$value
),
tension = 0.5
@@ -144,7 +149,8 @@ networkplot <- function(R, groups = NULL, title = NULL){
high = 'blue',
midpoint = 0,
limits = c(-1, 1),
- guide = 'none'
+ name = r_label,
+ guide = ggplot2::guide_colourbar()
) +
# operates on node data from the layout
# leaf is a logical vector (provided by the ggraph object g)
@@ -153,16 +159,19 @@ networkplot <- function(R, groups = NULL, title = NULL){
ggraph::geom_node_point(
ggplot2::aes(
filter = .data$leaf,
- fill = .data$group
+ colour = .data$group
),
- size = 1
+ size = 2
) +
# labels nodes and groups
ggraph::geom_node_text(
- ggplot2::aes(label = .data$name),
+ ggplot2::aes(
+ filter = .data$leaf,
+ label = .data$name
+ ),
# prevents text labels from overlapping
- repel = TRUE,
- size = 1
+ hjust = 'outward',
+ size = 2
) +
ggplot2::theme_void()
@@ -176,7 +185,7 @@ networkplot <- function(R, groups = NULL, title = NULL){
if(!is.null(groups)){
p <- p +
- ggplot2::scale_fill_discrete(name = 'Superpathway')
+ ggplot2::scale_colour_discrete(name = group_label)
}
return(p)