From 560aead33a48167d2f37bde192cd2b0767b73e43 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Wed, 6 Jan 2021 13:36:29 +0000 Subject: [PATCH 01/12] docs: add plot example for keymetrics_scan --- R/keymetrics_scan.R | 4 ++++ man/keymetrics_scan.Rd | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/R/keymetrics_scan.R b/R/keymetrics_scan.R index 36170a66..232c5ff6 100644 --- a/R/keymetrics_scan.R +++ b/R/keymetrics_scan.R @@ -26,6 +26,10 @@ #' When 'table' is passed, a summary table is returned as a data frame. #' #' @examples +#' ## Heatmap plot is returned by default +#' keymetrics_scan(sq_data) +#' +#' ## Return a table #' keymetrics_scan(sq_data, hrvar = "LevelDesignation", return = "table") #' #' @export diff --git a/man/keymetrics_scan.Rd b/man/keymetrics_scan.Rd index bc37afdc..4e0b88b5 100644 --- a/man/keymetrics_scan.Rd +++ b/man/keymetrics_scan.Rd @@ -41,6 +41,10 @@ When 'table' is passed, a summary table is returned as a data frame. Returns a heatmapped table by default, with options to return a table. } \examples{ +## Heatmap plot is returned by default +keymetrics_scan(sq_data) + +## Return a table keymetrics_scan(sq_data, hrvar = "LevelDesignation", return = "table") } From 25848eed217bb40fb85978e246b96e00a13d75b6 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Wed, 6 Jan 2021 13:53:26 +0000 Subject: [PATCH 02/12] docs: update NEWS.md --- NEWS.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index bc34e4c6..b7245375 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,25 @@ +# wpa 1.3.1 + +New functions, bug fixes, and performance improvements. + +Significant changes to existing functions: +- New plot visual is available for `keymetrics_scan()` +- `combine_signals()` can now dynamically accept any metrics available in the Hourly Collaboration query. +- `pairwise_count()` now uses a **data.table** implementation, instead of dependent on **widyr**. + +New functions: +- `network_p2p()` +- `network_leiden()` +- `network_louvain()` +- `network_describe()` +- `create_sankey()` +- `totals_col()` + +Some package dependencies have been removed (see #36): +- **network** +- **GGally** +- **widyr** + # wpa 1.3.0 -This is the first version of the **wpa** package to be released open-source on GitHub. If you have been using a previous developmental version, the main difference is that this release omits the more experimental _working patterns_ family of functions. The experimental functions are currently available upon request via mac@microsoft.com. \ No newline at end of file +This is the first version of the **wpa** package to be released open-source on GitHub. If you have been using a previous developmental version, the main difference is that this release omits the more experimental _working patterns_ family of functions. The experimental functions are currently available upon request via mac@microsoft.com. From 2ff3729d6fe7396543798a9ce8cfda6dd1a438b5 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Wed, 6 Jan 2021 13:53:50 +0000 Subject: [PATCH 03/12] docs: specify return as PDF --- R/network_leiden.R | 4 ++-- R/network_louvain.R | 4 ++-- man/network_leiden.Rd | 4 ++-- man/network_louvain.Rd | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 99163c88..a3b23e02 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -29,8 +29,8 @@ #' @param desc_hrvar Character vector of length 3 containing the HR attributes to use when returning the #' "describe" output. See `network_describe()`. #' @param return String specifying what output to return. Valid return options include: -#' - 'plot-leiden': return a network plot coloured by leiden communities. -#' - 'plot-hrvar': return a network plot coloured by HR attribute. +#' - 'plot-leiden': return a network plot coloured by leiden communities, saving a PDF to `path`. +#' - 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to `path`. #' - 'plot-sankey': return a sankey plot combining communities and HR attribute. #' - 'table': return a vertex summary table with counts in communities and HR attribute. #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. diff --git a/R/network_louvain.R b/R/network_louvain.R index f8714bbf..a430b952 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -26,8 +26,8 @@ #' "describe" output. See `network_describe()`. #' #' @param return String specifying what output to return.Valid return options include: -#' - 'plot-louvain': return a network plot coloured by louvain communities. -#' - 'plot-hrvar': return a network plot coloured by HR attribute. +#' - 'plot-louvain': return a network plot coloured by louvain communities, saving a PDF to `path`. +#' - 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to `path`. #' - 'plot-sankey': return a sankey plot combining communities and HR attribute. #' - 'table': return a vertex summary table with counts in communities and HR attribute. #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. diff --git a/man/network_leiden.Rd b/man/network_leiden.Rd index 7fe24514..c6706ffb 100644 --- a/man/network_leiden.Rd +++ b/man/network_leiden.Rd @@ -45,8 +45,8 @@ if returning anything other than "plot-leiden" or "plot-hrvar".} \item{return}{String specifying what output to return. Valid return options include: \itemize{ -\item 'plot-leiden': return a network plot coloured by leiden communities. -\item 'plot-hrvar': return a network plot coloured by HR attribute. +\item 'plot-leiden': return a network plot coloured by leiden communities, saving a PDF to \code{path}. +\item 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to \code{path}. \item 'plot-sankey': return a sankey plot combining communities and HR attribute. \item 'table': return a vertex summary table with counts in communities and HR attribute. \item 'data': return a vertex data file that matches vertices with communities and HR attributes. diff --git a/man/network_louvain.Rd b/man/network_louvain.Rd index c5b060e5..3a37eaf3 100644 --- a/man/network_louvain.Rd +++ b/man/network_louvain.Rd @@ -42,8 +42,8 @@ if returning anything other than "plot-louvain" or "plot-hrvar".} \item{return}{String specifying what output to return.Valid return options include: \itemize{ -\item 'plot-louvain': return a network plot coloured by louvain communities. -\item 'plot-hrvar': return a network plot coloured by HR attribute. +\item 'plot-louvain': return a network plot coloured by louvain communities, saving a PDF to \code{path}. +\item 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to \code{path}. \item 'plot-sankey': return a sankey plot combining communities and HR attribute. \item 'table': return a vertex summary table with counts in communities and HR attribute. \item 'data': return a vertex data file that matches vertices with communities and HR attributes. From d32e9f85fdd13e7da7f8144d21369ca0a8060459 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Wed, 6 Jan 2021 17:29:43 +0000 Subject: [PATCH 04/12] docs: enrich network docs Add default return values --- R/network_leiden.R | 11 +++++++---- R/network_louvain.R | 11 +++++++---- man/network_leiden.Rd | 13 ++++++++----- man/network_louvain.Rd | 13 ++++++++----- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index a3b23e02..a3c24245 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -10,8 +10,10 @@ #' this function, you will require all the pre-requisites of the **leiden** package installed, #' which includes Python and **reticulate**. #' -#' @param data Data frame containing a Person to Person query. +#' @param data Data frame containing a Person to Person Network query. Note that this function is +#' computationally intensive and may take a noticeably longer time to process beyond 5000 rows. #' @param hrvar String containing the HR attribute to be matched in the dataset. +#' Defaults to "Organization". #' @param bg_fill String to specify background fill colour. #' @param font_col String to specify font and link colour. #' @param node_alpha A numeric value between 0 and 1 to specify the transparency of the nodes. @@ -28,7 +30,8 @@ #' @param res Resolution parameter to be passed to `leiden::leiden()`. Defaults to 0.5. #' @param desc_hrvar Character vector of length 3 containing the HR attributes to use when returning the #' "describe" output. See `network_describe()`. -#' @param return String specifying what output to return. Valid return options include: +#' @param return String specifying what output to return. Defaults to "plot-leiden". +#' Valid return options include: #' - 'plot-leiden': return a network plot coloured by leiden communities, saving a PDF to `path`. #' - 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to `path`. #' - 'plot-sankey': return a sankey plot combining communities and HR attribute. @@ -41,7 +44,7 @@ #' #' @export network_leiden <- function(data, - hrvar, + hrvar = "Organization", bg_fill = "#000000", font_col = "#FFFFFF", algorithm = "mds", @@ -49,7 +52,7 @@ network_leiden <- function(data, node_alpha = 0.8, res = 0.5, desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return){ + return = "plot-leiden"){ ## Set variables TO_hrvar <- paste0("TieOrigin_", hrvar) diff --git a/R/network_louvain.R b/R/network_louvain.R index a430b952..73614543 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -9,8 +9,10 @@ #' Take a P2P network query and implement the Louvain community detection method. The #' **igraph** implementation of the Louvain method is used. #' -#' @param data Data frame containing a Person to Person query. +#' @param data Data frame containing a Person to Person Network query. Note that this function is +#' computationally intensive and may take a noticeably longer time to process beyond 5000 rows. #' @param hrvar String containing the HR attribute to be matched in the dataset. +#' Defaults to "Organization". #' @param bg_fill String to specify background fill colour. #' @param font_col String to specify font and link colour. #' @param node_alpha A numeric value between 0 and 1 to specify the transparency of the nodes. @@ -25,7 +27,8 @@ #' @param desc_hrvar Character vector of length 3 containing the HR attributes to use when returning the #' "describe" output. See `network_describe()`. #' -#' @param return String specifying what output to return.Valid return options include: +#' @param return String specifying what output to return. Defaults to "plot-louvain". +#' Valid return options include: #' - 'plot-louvain': return a network plot coloured by louvain communities, saving a PDF to `path`. #' - 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to `path`. #' - 'plot-sankey': return a sankey plot combining communities and HR attribute. @@ -39,14 +42,14 @@ #' #' @export network_louvain <- function(data, - hrvar, + hrvar = "Organization", bg_fill = "#000000", font_col = "#FFFFFF", node_alpha = 0.8, algorithm = "mds", path = "network_p2p_louvain", desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return){ + return = "plot-louvain"){ ## Set variables TO_hrvar <- paste0("TieOrigin_", hrvar) diff --git a/man/network_leiden.Rd b/man/network_leiden.Rd index c6706ffb..121c72a5 100644 --- a/man/network_leiden.Rd +++ b/man/network_leiden.Rd @@ -6,7 +6,7 @@ \usage{ network_leiden( data, - hrvar, + hrvar = "Organization", bg_fill = "#000000", font_col = "#FFFFFF", algorithm = "mds", @@ -14,13 +14,15 @@ network_leiden( node_alpha = 0.8, res = 0.5, desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return + return = "plot-leiden" ) } \arguments{ -\item{data}{Data frame containing a Person to Person query.} +\item{data}{Data frame containing a Person to Person Network query. Note that this function is +computationally intensive and may take a noticeably longer time to process beyond 5000 rows.} -\item{hrvar}{String containing the HR attribute to be matched in the dataset.} +\item{hrvar}{String containing the HR attribute to be matched in the dataset. +Defaults to "Organization".} \item{bg_fill}{String to specify background fill colour.} @@ -43,7 +45,8 @@ if returning anything other than "plot-leiden" or "plot-hrvar".} \item{desc_hrvar}{Character vector of length 3 containing the HR attributes to use when returning the "describe" output. See \code{network_describe()}.} -\item{return}{String specifying what output to return. Valid return options include: +\item{return}{String specifying what output to return. Defaults to "plot-leiden". +Valid return options include: \itemize{ \item 'plot-leiden': return a network plot coloured by leiden communities, saving a PDF to \code{path}. \item 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to \code{path}. diff --git a/man/network_louvain.Rd b/man/network_louvain.Rd index 3a37eaf3..cdc94810 100644 --- a/man/network_louvain.Rd +++ b/man/network_louvain.Rd @@ -6,20 +6,22 @@ \usage{ network_louvain( data, - hrvar, + hrvar = "Organization", bg_fill = "#000000", font_col = "#FFFFFF", node_alpha = 0.8, algorithm = "mds", path = "network_p2p_louvain", desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return + return = "plot-louvain" ) } \arguments{ -\item{data}{Data frame containing a Person to Person query.} +\item{data}{Data frame containing a Person to Person Network query. Note that this function is +computationally intensive and may take a noticeably longer time to process beyond 5000 rows.} -\item{hrvar}{String containing the HR attribute to be matched in the dataset.} +\item{hrvar}{String containing the HR attribute to be matched in the dataset. +Defaults to "Organization".} \item{bg_fill}{String to specify background fill colour.} @@ -40,7 +42,8 @@ if returning anything other than "plot-louvain" or "plot-hrvar".} \item{desc_hrvar}{Character vector of length 3 containing the HR attributes to use when returning the "describe" output. See \code{network_describe()}.} -\item{return}{String specifying what output to return.Valid return options include: +\item{return}{String specifying what output to return. Defaults to "plot-louvain". +Valid return options include: \itemize{ \item 'plot-louvain': return a network plot coloured by louvain communities, saving a PDF to \code{path}. \item 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to \code{path}. From d53e84a3f91b6f4e297a87103889983f11eeecd3 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 10:17:04 +0000 Subject: [PATCH 05/12] feat: add fast plotting method Only implemented for network_leiden() --- NAMESPACE | 1 + R/network_leiden.R | 197 ++++++++++++++++++++++++++++++++---------- man/network_leiden.Rd | 8 +- 3 files changed, 158 insertions(+), 48 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index bb223eb1..675546a9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -171,6 +171,7 @@ importFrom(dplyr,mutate_if) importFrom(grDevices,rainbow) importFrom(htmltools,HTML) importFrom(igraph,graph_from_data_frame) +importFrom(igraph,plot.igraph) importFrom(magrittr,"%>%") importFrom(markdown,markdownToHTML) importFrom(methods,is) diff --git a/R/network_leiden.R b/R/network_leiden.R index a3c24245..9347225a 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -39,8 +39,13 @@ #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. #' - 'describe': return a list of data frames which describe each of the identified communities. #' - 'network': return igraph object. +#' @param size_threshold Numeric value representing the maximum number of edges before `network_leiden()` +#' switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. +#' Set as `0` to co-erce to a fast plotting method every time, and `Inf` to always use the default plotting +#' method. #' #' @import dplyr +#' @importFrom igraph plot.igraph #' #' @export network_leiden <- function(data, @@ -52,7 +57,8 @@ network_leiden <- function(data, node_alpha = 0.8, res = 0.5, desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return = "plot-leiden"){ + return = "plot-leiden", + size_threshold = 5000){ ## Set variables TO_hrvar <- paste0("TieOrigin_", hrvar) @@ -115,67 +121,164 @@ network_leiden <- function(data, ## Return if(return == "plot-leiden"){ - plot_output <- - g_layout + - ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + - ggraph::geom_node_point(aes(colour = cluster), - alpha = node_alpha, - pch = 16) + - theme_void() + - theme(legend.position = "bottom", - legend.background = element_rect(fill = bg_fill), - plot.background = element_rect(fill = bg_fill), - text = element_text(colour = font_col), - axis.line = element_blank()) + - labs(title = "Person to person collaboration with Community Detection", - subtitle = "Based on Leiden algorithm and Strong Tie Score", - y = "", - x = "") + if(igraph::ecount(g) > 5000){ - # Default PDF output unless NULL supplied to path - if(is.null(path)){ + message("Using fast plot method due to large network size...") - plot_output + ## Set colours + colour_tb <- + tibble(cluster = unique(igraph::V(g)$cluster)) %>% + mutate(colour = rainbow(nrow(.))) + + ## Colour vector + colour_v <- + tibble(cluster = igraph::V(g)$cluster) %>% + left_join(colour_tb, by = "cluster") %>% + pull(colour) + + + igraph::V(g)$color <- grDevices::adjustcolor(colour_v, alpha.f = node_alpha) + igraph::V(g)$frame.color <- NA + igraph::E(g)$width <- 1 + + grDevices::pdf(paste0(path, tstamp(), ".pdf")) + + par(bg = bg_fill) + + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") + + legend(x = -1.5, + y = 0.5, + legend = colour_tb$cluster, + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg= colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + + grDevices::dev.off() } else { - ggsave(paste0(path, tstamp(), ".pdf"), - plot = plot_output, - width = 16, - height = 9) + plot_output <- + g_layout + + ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + + ggraph::geom_node_point(aes(colour = cluster), + alpha = node_alpha, + pch = 16) + + theme_void() + + theme(legend.position = "bottom", + legend.background = element_rect(fill = bg_fill), + plot.background = element_rect(fill = bg_fill), + text = element_text(colour = font_col), + axis.line = element_blank()) + + labs(title = "Person to person collaboration with Community Detection", + subtitle = "Based on Leiden algorithm and Strong Tie Score", + y = "", + x = "") + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_output + + } else { + + ggsave(paste0(path, tstamp(), ".pdf"), + plot = plot_output, + width = 16, + height = 9) + + } } } else if(return == "plot-hrvar"){ - plot_output <- - g_layout + - ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + - ggraph::geom_node_point(aes(colour = !!sym(hrvar)), - alpha = node_alpha, - pch = 16) + - theme_void() + - theme(legend.position = "bottom", - legend.background = element_rect(fill = bg_fill), - plot.background = element_rect(fill = bg_fill), - text = element_text(colour = font_col), - axis.line = element_blank()) + - labs(title = "Person to person collaboration", - subtitle = paste0("Showing ", hrvar), - y = "", - x = "") + if(igraph::ecount(g) > 5000){ - # Default PDF output unless NULL supplied to path - if(is.null(path)){ + message("Using fast plot method due to large network size...") - plot_output + ## Set colours + colour_tb <- + tibble(!!sym(hrvar) := unique(igraph::get.vertex.attribute(g, name = hrvar))) %>% + mutate(colour = rainbow(nrow(.))) + + ## Colour vector + colour_v <- + tibble(!!sym(hrvar) := igraph::get.vertex.attribute(g, name = hrvar)) %>% + left_join(colour_tb, by = hrvar) %>% + pull(colour) + + + igraph::V(g)$color <- grDevices::adjustcolor(colour_v, alpha.f = node_alpha) + igraph::V(g)$frame.color <- NA + igraph::E(g)$width <- 1 + + grDevices::pdf(paste0(path, tstamp(), ".pdf")) + + par(bg = bg_fill) + + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") + + legend(x = -1.5, + y = 0.5, + legend = colour_tb[[hrvar]], + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg = colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + + grDevices::dev.off() } else { - ggsave(paste0(path, tstamp(), ".pdf"), - plot = plot_output, - width = 16, - height = 9) + plot_output <- + g_layout + + ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + + ggraph::geom_node_point(aes(colour = !!sym(hrvar)), + alpha = node_alpha, + pch = 16) + + theme_void() + + theme(legend.position = "bottom", + legend.background = element_rect(fill = bg_fill), + plot.background = element_rect(fill = bg_fill), + text = element_text(colour = font_col), + axis.line = element_blank()) + + labs(title = "Person to person collaboration", + subtitle = paste0("Showing ", hrvar), + y = "", + x = "") + + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_output + + } else { + + ggsave(paste0(path, tstamp(), ".pdf"), + plot = plot_output, + width = 16, + height = 9) + + } } diff --git a/man/network_leiden.Rd b/man/network_leiden.Rd index 121c72a5..e374487c 100644 --- a/man/network_leiden.Rd +++ b/man/network_leiden.Rd @@ -14,7 +14,8 @@ network_leiden( node_alpha = 0.8, res = 0.5, desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return = "plot-leiden" + return = "plot-leiden", + size_threshold = 5000 ) } \arguments{ @@ -56,6 +57,11 @@ Valid return options include: \item 'describe': return a list of data frames which describe each of the identified communities. \item 'network': return igraph object. }} + +\item{size_threshold}{Numeric value representing the maximum number of edges before \code{network_leiden()} +switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. +Set as \code{0} to co-erce to a fast plotting method every time, and \code{Inf} to always use the default plotting +method.} } \description{ Take a P2P network query and implement the Leiden community detection method. To run From cb15a994534e41ae1d1eac83d0008bdd699b95bf Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 10:37:16 +0000 Subject: [PATCH 06/12] feat: add "Saved to" message Confirms to the user where the output plot is saved. A size threshold is also added. --- R/network_leiden.R | 7 ++ R/network_louvain.R | 206 +++++++++++++++++++++++++++++++---------- man/network_louvain.Rd | 8 +- 3 files changed, 173 insertions(+), 48 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 9347225a..a1c500aa 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -118,6 +118,9 @@ network_leiden <- function(data, g %>% ggraph::ggraph(layout = "igraph", algorithm = algorithm) + ## Timestamped File Path + out_path <- paste0(path, tstamp(), ".pdf") + ## Return if(return == "plot-leiden"){ @@ -200,6 +203,8 @@ network_leiden <- function(data, } } + message(paste0("Saved to ", out_path, ".")) + } else if(return == "plot-hrvar"){ if(igraph::ecount(g) > 5000){ @@ -282,6 +287,8 @@ network_leiden <- function(data, } + message(paste0("Saved to ", out_path, ".")) + } else if(return == "table"){ vertex_tb %>% diff --git a/R/network_louvain.R b/R/network_louvain.R index 73614543..a1937cf9 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -36,9 +36,14 @@ #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. #' - 'describe': returns a list of data frames which describe each of the identified communities. #' - 'network': return igraph object. +#' @param size_threshold Numeric value representing the maximum number of edges before `network_leiden()` +#' switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. +#' Set as `0` to co-erce to a fast plotting method every time, and `Inf` to always use the default plotting +#' method. #' #' @import ggraph #' @import dplyr +#' @importFrom igraph plot.igraph #' #' @export network_louvain <- function(data, @@ -49,7 +54,8 @@ network_louvain <- function(data, algorithm = "mds", path = "network_p2p_louvain", desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return = "plot-louvain"){ + return = "plot-louvain", + size_threshold = 5000){ ## Set variables TO_hrvar <- paste0("TieOrigin_", hrvar) @@ -109,73 +115,179 @@ network_louvain <- function(data, g %>% ggraph::ggraph(layout = "igraph", algorithm = algorithm) + ## Timestamped File Path + out_path <- paste0(path, tstamp(), ".pdf") + ## Return if(return == "plot-louvain"){ - plot_output <- - g_layout + - ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + - ggraph::geom_node_point(aes(colour = cluster), - alpha = node_alpha, - pch = 16) + - theme_void() + - theme(legend.position = "bottom", - legend.background = element_rect(fill = bg_fill), - plot.background = element_rect(fill = bg_fill), - text = element_text(colour = font_col), - axis.line = element_blank()) + - labs(title = "Person to person collaboration with Community Detection", - subtitle = "Based on Louvain algorithm and Strong Tie Score", - y = "", - x = "") + if(igraph::ecount(g) > 5000){ - # Default PDF output unless NULL supplied to path - if(is.null(path)){ + message("Using fast plot method due to large network size...") - plot_output + ## Set colours + colour_tb <- + tibble(cluster = unique(igraph::V(g)$cluster)) %>% + mutate(colour = rainbow(nrow(.))) + + ## Colour vector + colour_v <- + tibble(cluster = igraph::V(g)$cluster) %>% + left_join(colour_tb, by = "cluster") %>% + pull(colour) + + + igraph::V(g)$color <- grDevices::adjustcolor(colour_v, alpha.f = node_alpha) + igraph::V(g)$frame.color <- NA + igraph::E(g)$width <- 1 + + grDevices::pdf(out_path) + + par(bg = bg_fill) + + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") + + legend(x = -1.5, + y = 0.5, + legend = colour_tb$cluster, + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg= colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + + grDevices::dev.off() } else { - ggsave(paste0(path, tstamp(), ".pdf"), - plot = plot_output, - width = 16, - height = 9) + plot_output <- + g_layout + + ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + + ggraph::geom_node_point(aes(colour = cluster), + alpha = node_alpha, + pch = 16) + + theme_void() + + theme(legend.position = "bottom", + legend.background = element_rect(fill = bg_fill), + plot.background = element_rect(fill = bg_fill), + text = element_text(colour = font_col), + axis.line = element_blank()) + + labs(title = "Person to person collaboration with Community Detection", + subtitle = "Based on Louvain algorithm and Strong Tie Score", + y = "", + x = "") + + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_output + + } else { + + ggsave(out_path, + plot = plot_output, + width = 16, + height = 9) + + } } + message(paste0("Saved to ", out_path, ".")) + + } else if(return == "plot-hrvar"){ - plot_output <- - g_layout + - ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + - ggraph::geom_node_point(aes(colour = !!sym(hrvar)), - alpha = node_alpha, - pch = 16) + - theme_void() + - theme(legend.position = "bottom", - legend.background = element_rect(fill = bg_fill), - plot.background = element_rect(fill = bg_fill), - text = element_text(colour = font_col), - axis.line = element_blank()) + - labs(title = "Person to person collaboration", - subtitle = paste0("Showing ", hrvar), - y = "", - x = "") + if(igraph::ecount(g) > 5000){ - # Default PDF output unless NULL supplied to path - if(is.null(path)){ + message("Using fast plot method due to large network size...") - plot_output + ## Set colours + colour_tb <- + tibble(!!sym(hrvar) := unique(igraph::get.vertex.attribute(g, name = hrvar))) %>% + mutate(colour = rainbow(nrow(.))) + + ## Colour vector + colour_v <- + tibble(!!sym(hrvar) := igraph::get.vertex.attribute(g, name = hrvar)) %>% + left_join(colour_tb, by = hrvar) %>% + pull(colour) + + + igraph::V(g)$color <- grDevices::adjustcolor(colour_v, alpha.f = node_alpha) + igraph::V(g)$frame.color <- NA + igraph::E(g)$width <- 1 + + grDevices::pdf(out_path) + + par(bg = bg_fill) + + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") + + legend(x = -1.5, + y = 0.5, + legend = colour_tb[[hrvar]], + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg = colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + + grDevices::dev.off() } else { - ggsave(paste0(path, tstamp(), ".pdf"), - plot = plot_output, - width = 16, - height = 9) + plot_output <- + g_layout + + ggraph::geom_edge_link(colour = "lightgrey", edge_width = 0.01, alpha = 0.15) + + ggraph::geom_node_point(aes(colour = !!sym(hrvar)), + alpha = node_alpha, + pch = 16) + + theme_void() + + theme(legend.position = "bottom", + legend.background = element_rect(fill = bg_fill), + plot.background = element_rect(fill = bg_fill), + text = element_text(colour = font_col), + axis.line = element_blank()) + + labs(title = "Person to person collaboration", + subtitle = paste0("Showing ", hrvar), + y = "", + x = "") + + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_output + + } else { + + ggsave(out_path, + plot = plot_output, + width = 16, + height = 9) + + } } + message(paste0("Saved to ", out_path, ".")) + } else if(return == "table"){ vertex_tb %>% diff --git a/man/network_louvain.Rd b/man/network_louvain.Rd index cdc94810..e579bd85 100644 --- a/man/network_louvain.Rd +++ b/man/network_louvain.Rd @@ -13,7 +13,8 @@ network_louvain( algorithm = "mds", path = "network_p2p_louvain", desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"), - return = "plot-louvain" + return = "plot-louvain", + size_threshold = 5000 ) } \arguments{ @@ -53,6 +54,11 @@ Valid return options include: \item 'describe': returns a list of data frames which describe each of the identified communities. \item 'network': return igraph object. }} + +\item{size_threshold}{Numeric value representing the maximum number of edges before \code{network_leiden()} +switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. +Set as \code{0} to co-erce to a fast plotting method every time, and \code{Inf} to always use the default plotting +method.} } \description{ Take a P2P network query and implement the Louvain community detection method. The From b1ad559564b6aa7d778645c60ef8c7974a871b00 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 10:40:35 +0000 Subject: [PATCH 07/12] fix: size_threshold Previously not properly implemented --- R/network_leiden.R | 4 ++-- R/network_louvain.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index a1c500aa..776b2077 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -124,7 +124,7 @@ network_leiden <- function(data, ## Return if(return == "plot-leiden"){ - if(igraph::ecount(g) > 5000){ + if(igraph::ecount(g) > size_threshold){ message("Using fast plot method due to large network size...") @@ -207,7 +207,7 @@ network_leiden <- function(data, } else if(return == "plot-hrvar"){ - if(igraph::ecount(g) > 5000){ + if(igraph::ecount(g) > size_threshold){ message("Using fast plot method due to large network size...") diff --git a/R/network_louvain.R b/R/network_louvain.R index a1937cf9..34c4f36f 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -121,7 +121,7 @@ network_louvain <- function(data, ## Return if(return == "plot-louvain"){ - if(igraph::ecount(g) > 5000){ + if(igraph::ecount(g) > size_threshold){ message("Using fast plot method due to large network size...") @@ -206,7 +206,7 @@ network_louvain <- function(data, } else if(return == "plot-hrvar"){ - if(igraph::ecount(g) > 5000){ + if(igraph::ecount(g) > size_threshold){ message("Using fast plot method due to large network size...") From d6bb198814e8fb07b55462543ac9071b5df26dcb Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 11:17:55 +0000 Subject: [PATCH 08/12] format: change title to caption --- R/network_leiden.R | 7 +++---- R/network_louvain.R | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 776b2077..414e8d8c 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -183,8 +183,8 @@ network_leiden <- function(data, plot.background = element_rect(fill = bg_fill), text = element_text(colour = font_col), axis.line = element_blank()) + - labs(title = "Person to person collaboration with Community Detection", - subtitle = "Based on Leiden algorithm and Strong Tie Score", + labs(caption = "Person to person collaboration with Community Detection + based on the Leiden algorithm. ", y = "", x = "") @@ -266,8 +266,7 @@ network_leiden <- function(data, plot.background = element_rect(fill = bg_fill), text = element_text(colour = font_col), axis.line = element_blank()) + - labs(title = "Person to person collaboration", - subtitle = paste0("Showing ", hrvar), + labs(caption = paste0("Person to person collaboration showing ", hrvar, ". "), # spaces intentional y = "", x = "") diff --git a/R/network_louvain.R b/R/network_louvain.R index 34c4f36f..dab53b59 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -180,8 +180,8 @@ network_louvain <- function(data, plot.background = element_rect(fill = bg_fill), text = element_text(colour = font_col), axis.line = element_blank()) + - labs(title = "Person to person collaboration with Community Detection", - subtitle = "Based on Louvain algorithm and Strong Tie Score", + labs(caption = "Person to person collaboration with Community Detection + based on the Louvain algorithm. ", y = "", x = "") @@ -265,8 +265,7 @@ network_louvain <- function(data, plot.background = element_rect(fill = bg_fill), text = element_text(colour = font_col), axis.line = element_blank()) + - labs(title = "Person to person collaboration", - subtitle = paste0("Showing ", hrvar), + labs(caption = paste0("Person to person collaboration showing ", hrvar, ". "), # spaces intentional y = "", x = "") From 6e5bc771f02a3d99a652d23ddedf671e1c82cb92 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 12:02:13 +0000 Subject: [PATCH 09/12] feat: add summary table option For network_louvain and network_leiden --- R/network_leiden.R | 27 ++++++++++++++++++++++++++- R/network_louvain.R | 27 ++++++++++++++++++++++++++- man/network_leiden.Rd | 1 + man/network_louvain.Rd | 1 + 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 414e8d8c..99bdb26f 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -38,6 +38,7 @@ #' - 'table': return a vertex summary table with counts in communities and HR attribute. #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. #' - 'describe': return a list of data frames which describe each of the identified communities. +#' The first data frame is a summary table of all the communities. #' - 'network': return igraph object. #' @param size_threshold Numeric value representing the maximum number of edges before `network_leiden()` #' switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. @@ -321,7 +322,8 @@ network_leiden <- function(data, pull(cluster) %>% unique() - desc_str %>% + out_list <- + desc_str %>% purrr::map(function(x){ describe_tb %>% filter(cluster == x) %>% @@ -329,6 +331,29 @@ network_leiden <- function(data, }) %>% setNames(nm = desc_str) + summaryTable <- + list(i = out_list, + j = names(out_list)) %>% + purrr::pmap(function(i, j){ + i %>% + arrange(desc(Percentage)) %>% + slice(1) %>% + mutate_at(vars(starts_with("feature_")), ~tidyr::replace_na(., "")) %>% + mutate(Community = j, + `Attribute 1` = paste(feature_1, "=", feature_1_value), + `Attribute 2` = paste(feature_2, "=", feature_2_value), + `Attribute 3` = paste(feature_3, "=", feature_3_value)) %>% + select(Community, + `Attribute 1`, + `Attribute 2`, + `Attribute 3`, + PercentageExplained = "Percentage") %>% + mutate_at(vars(starts_with("Attribute")), ~ifelse(. == " = ", NA, .)) + }) %>% + bind_rows() + + c(list("summaryTable" = summaryTable), out_list) + } else { stop("Please enter a valid input for `return`.") diff --git a/R/network_louvain.R b/R/network_louvain.R index dab53b59..ffa31719 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -35,6 +35,7 @@ #' - 'table': return a vertex summary table with counts in communities and HR attribute. #' - 'data': return a vertex data file that matches vertices with communities and HR attributes. #' - 'describe': returns a list of data frames which describe each of the identified communities. +#' The first data frame is a summary table of all the communities. #' - 'network': return igraph object. #' @param size_threshold Numeric value representing the maximum number of edges before `network_leiden()` #' switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. @@ -320,7 +321,8 @@ network_louvain <- function(data, pull(cluster) %>% unique() - desc_str %>% + out_list <- + desc_str %>% purrr::map(function(x){ describe_tb %>% filter(cluster == x) %>% @@ -328,6 +330,29 @@ network_louvain <- function(data, }) %>% setNames(nm = desc_str) + summaryTable <- + list(i = out_list, + j = names(out_list)) %>% + purrr::pmap(function(i, j){ + i %>% + arrange(desc(Percentage)) %>% + slice(1) %>% + mutate_at(vars(starts_with("feature_")), ~tidyr::replace_na(., "")) %>% + mutate(Community = j, + `Attribute 1` = paste(feature_1, "=", feature_1_value), + `Attribute 2` = paste(feature_2, "=", feature_2_value), + `Attribute 3` = paste(feature_3, "=", feature_3_value)) %>% + select(Community, + `Attribute 1`, + `Attribute 2`, + `Attribute 3`, + PercentageExplained = "Percentage") %>% + mutate_at(vars(starts_with("Attribute")), ~ifelse(. == " = ", NA, .)) + }) %>% + bind_rows() + + c(list("summaryTable" = summaryTable), out_list) + } else { stop("Please enter a valid input for `return`.") diff --git a/man/network_leiden.Rd b/man/network_leiden.Rd index e374487c..bc5d494d 100644 --- a/man/network_leiden.Rd +++ b/man/network_leiden.Rd @@ -55,6 +55,7 @@ Valid return options include: \item 'table': return a vertex summary table with counts in communities and HR attribute. \item 'data': return a vertex data file that matches vertices with communities and HR attributes. \item 'describe': return a list of data frames which describe each of the identified communities. +The first data frame is a summary table of all the communities. \item 'network': return igraph object. }} diff --git a/man/network_louvain.Rd b/man/network_louvain.Rd index e579bd85..ad6b9811 100644 --- a/man/network_louvain.Rd +++ b/man/network_louvain.Rd @@ -52,6 +52,7 @@ Valid return options include: \item 'table': return a vertex summary table with counts in communities and HR attribute. \item 'data': return a vertex data file that matches vertices with communities and HR attributes. \item 'describe': returns a list of data frames which describe each of the identified communities. +The first data frame is a summary table of all the communities. \item 'network': return igraph object. }} From 4cd8ec70b220e358839e9e3117b6750e9b32ccb3 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 13:51:49 +0000 Subject: [PATCH 10/12] feat: allow console plotting --- R/network_louvain.R | 119 ++++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 44 deletions(-) diff --git a/R/network_louvain.R b/R/network_louvain.R index ffa31719..3e79bfbe 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -45,6 +45,7 @@ #' @import ggraph #' @import dplyr #' @importFrom igraph plot.igraph +#' @importFrom igraph layout_with_mds #' #' @export network_louvain <- function(data, @@ -142,30 +143,44 @@ network_louvain <- function(data, igraph::V(g)$frame.color <- NA igraph::E(g)$width <- 1 - grDevices::pdf(out_path) + plot_cluster <- function(){ - par(bg = bg_fill) + par(bg = bg_fill) - plot(g, - layout = layout_with_mds, - vertex.label = NA, - vertex.size = 3, - edge.arrow.mode = "-", - edge.color = "#adadad") + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") - legend(x = -1.5, - y = 0.5, - legend = colour_tb$cluster, - pch = 21, - text.col = font_col, - col = "#777777", - pt.bg= colour_tb$colour, - pt.cex = 2, - cex = .8, - bty = "n", - ncol = 1) + legend(x = -1.5, + y = 0.5, + legend = colour_tb$cluster, + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg= colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + } - grDevices::dev.off() + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_cluster() + + } else { + + grDevices::pdf(out_path) + + plot_cluster() + + message(paste0("Saved to ", out_path, ".")) + + } } else { @@ -198,13 +213,12 @@ network_louvain <- function(data, width = 16, height = 9) + message(paste0("Saved to ", out_path, ".")) + } } - message(paste0("Saved to ", out_path, ".")) - - } else if(return == "plot-hrvar"){ if(igraph::ecount(g) > size_threshold){ @@ -227,30 +241,46 @@ network_louvain <- function(data, igraph::V(g)$frame.color <- NA igraph::E(g)$width <- 1 - grDevices::pdf(out_path) + plot_hrvar <- function(){ - par(bg = bg_fill) + par(bg = bg_fill) - plot(g, - layout = layout_with_mds, - vertex.label = NA, - vertex.size = 3, - edge.arrow.mode = "-", - edge.color = "#adadad") + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") - legend(x = -1.5, - y = 0.5, - legend = colour_tb[[hrvar]], - pch = 21, - text.col = font_col, - col = "#777777", - pt.bg = colour_tb$colour, - pt.cex = 2, - cex = .8, - bty = "n", - ncol = 1) + legend(x = -1.5, + y = 0.5, + legend = colour_tb[[hrvar]], + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg = colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) + } - grDevices::dev.off() + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_hrvar() + + } else { + + grDevices::pdf(out_path) + + plot_hrvar() + + grDevices::dev.off() + + message(paste0("Saved to ", out_path, ".")) + + } } else { @@ -284,9 +314,10 @@ network_louvain <- function(data, } + message(paste0("Saved to ", out_path, ".")) + } - message(paste0("Saved to ", out_path, ".")) } else if(return == "table"){ From e4766967d8b20bceae77fc09e8ba9a1f5386b9d0 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 13:56:25 +0000 Subject: [PATCH 11/12] fix: allow console plotting --- R/network_leiden.R | 121 ++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 44 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 99bdb26f..24831331 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -47,6 +47,7 @@ #' #' @import dplyr #' @importFrom igraph plot.igraph +#' @importFrom igraph layout_with_mds #' #' @export network_leiden <- function(data, @@ -145,30 +146,45 @@ network_leiden <- function(data, igraph::V(g)$frame.color <- NA igraph::E(g)$width <- 1 - grDevices::pdf(paste0(path, tstamp(), ".pdf")) + plot_cluster <- function(){ - par(bg = bg_fill) + par(bg = bg_fill) - plot(g, - layout = layout_with_mds, - vertex.label = NA, - vertex.size = 3, - edge.arrow.mode = "-", - edge.color = "#adadad") + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") - legend(x = -1.5, - y = 0.5, - legend = colour_tb$cluster, - pch = 21, - text.col = font_col, - col = "#777777", - pt.bg= colour_tb$colour, - pt.cex = 2, - cex = .8, - bty = "n", - ncol = 1) + legend(x = -1.5, + y = 0.5, + legend = colour_tb$cluster, + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg= colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) - grDevices::dev.off() + } + + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_cluster() + + } else { + + grDevices::pdf(out_path) + + plot_cluster() + + message(paste0("Saved to ", out_path, ".")) + + } } else { @@ -201,11 +217,11 @@ network_leiden <- function(data, width = 16, height = 9) + message(paste0("Saved to ", out_path, ".")) + } } - message(paste0("Saved to ", out_path, ".")) - } else if(return == "plot-hrvar"){ if(igraph::ecount(g) > size_threshold){ @@ -228,30 +244,47 @@ network_leiden <- function(data, igraph::V(g)$frame.color <- NA igraph::E(g)$width <- 1 - grDevices::pdf(paste0(path, tstamp(), ".pdf")) + plot_hrvar <- function(){ - par(bg = bg_fill) + par(bg = bg_fill) - plot(g, - layout = layout_with_mds, - vertex.label = NA, - vertex.size = 3, - edge.arrow.mode = "-", - edge.color = "#adadad") + plot(g, + layout = layout_with_mds, + vertex.label = NA, + vertex.size = 3, + edge.arrow.mode = "-", + edge.color = "#adadad") - legend(x = -1.5, - y = 0.5, - legend = colour_tb[[hrvar]], - pch = 21, - text.col = font_col, - col = "#777777", - pt.bg = colour_tb$colour, - pt.cex = 2, - cex = .8, - bty = "n", - ncol = 1) + legend(x = -1.5, + y = 0.5, + legend = colour_tb[[hrvar]], + pch = 21, + text.col = font_col, + col = "#777777", + pt.bg = colour_tb$colour, + pt.cex = 2, + cex = .8, + bty = "n", + ncol = 1) - grDevices::dev.off() + } + + # Default PDF output unless NULL supplied to path + if(is.null(path)){ + + plot_hrvar() + + } else { + + grDevices::pdf(out_path) + + plot_hrvar() + + grDevices::dev.off() + + message(paste0("Saved to ", out_path, ".")) + + } } else { @@ -283,12 +316,12 @@ network_leiden <- function(data, width = 16, height = 9) + message(paste0("Saved to ", out_path, ".")) + } } - message(paste0("Saved to ", out_path, ".")) - } else if(return == "table"){ vertex_tb %>% From b58dd01a7afc43ffcb00e0eeaf4841b1fda55524 Mon Sep 17 00:00:00 2001 From: Martin Chan Date: Thu, 7 Jan 2021 17:42:24 +0000 Subject: [PATCH 12/12] chore: add explicit namespace --- R/network_leiden.R | 4 ++-- R/network_louvain.R | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/network_leiden.R b/R/network_leiden.R index 24831331..4854731f 100644 --- a/R/network_leiden.R +++ b/R/network_leiden.R @@ -151,7 +151,7 @@ network_leiden <- function(data, par(bg = bg_fill) plot(g, - layout = layout_with_mds, + layout = igraph::layout_with_mds, vertex.label = NA, vertex.size = 3, edge.arrow.mode = "-", @@ -249,7 +249,7 @@ network_leiden <- function(data, par(bg = bg_fill) plot(g, - layout = layout_with_mds, + layout = igraph::layout_with_mds, vertex.label = NA, vertex.size = 3, edge.arrow.mode = "-", diff --git a/R/network_louvain.R b/R/network_louvain.R index 3e79bfbe..93b01e16 100644 --- a/R/network_louvain.R +++ b/R/network_louvain.R @@ -148,7 +148,7 @@ network_louvain <- function(data, par(bg = bg_fill) plot(g, - layout = layout_with_mds, + layout = igraph::layout_with_mds, vertex.label = NA, vertex.size = 3, edge.arrow.mode = "-", @@ -246,7 +246,7 @@ network_louvain <- function(data, par(bg = bg_fill) plot(g, - layout = layout_with_mds, + layout = igraph::layout_with_mds, vertex.label = NA, vertex.size = 3, edge.arrow.mode = "-",