From 6a934b3dc2f707696c6743ef049f61a26aa134f0 Mon Sep 17 00:00:00 2001 From: lilyclements Date: Wed, 7 Feb 2024 15:54:17 +0000 Subject: [PATCH] Adding map_data R function --- NAMESPACE | 1 + R/Functions.R | 1 + R/initiative_by.R | 1 - R/map_data.R | 62 ++++++++++++++++++++++++++++++++ man/initiative_by.Rd | 86 ++++++++++++++++++++++++++++++++++++++++++++ man/map_data.Rd | 36 +++++++++++++++++++ 6 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 R/map_data.R create mode 100644 man/initiative_by.Rd create mode 100644 man/map_data.Rd diff --git a/NAMESPACE b/NAMESPACE index ea39623..21100fc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(map_data) importFrom(magrittr,"%>%") diff --git a/R/Functions.R b/R/Functions.R index ace0b64..36d4ba6 100644 --- a/R/Functions.R +++ b/R/Functions.R @@ -272,6 +272,7 @@ individuals_by <- function(individual_data = individuals, ind_id = nom, group = clickAction = click_action) } } + replace_other <- function(data = individuals, group = NULL, group_other = paste0("autre_", group), group_other_name = NULL, RAS = FALSE){ group_var <- group # if (group_other == "autre_pays"){ diff --git a/R/initiative_by.R b/R/initiative_by.R index 574587d..d1cb623 100644 --- a/R/initiative_by.R +++ b/R/initiative_by.R @@ -27,7 +27,6 @@ #' @param click_action #' #' @return Returns a network graph object -#' @export #' #' @examples # todo initiative_by <- function(initiative_data, by = "pays", filter_var = NULL, filter_vals = NULL, diff --git a/R/map_data.R b/R/map_data.R new file mode 100644 index 0000000..bcd3926 --- /dev/null +++ b/R/map_data.R @@ -0,0 +1,62 @@ +#' Map Data for Network Visualisation +#' +#' This function takes a data frame and generates a network visualization using the \code{forceNetwork} function +#' from the \code{networkD3} package. It separates and expands the data based on the specified values and node values, +#' and creates links between them. +#' +#' @param data The original data frame. +#' @param values The column name representing the primary values to be mapped. +#' @param node_values A character vector specifying the column names representing the node values. +#' +#' @return A network visualisation generated using the \code{forceNetwork} function. +#' @export +#' +#' @examples +#' # An example with simple data +#' df_original <- data.frame(initiative = c("A", "B", "C", "D"), +#' country = c("BF", "BF Niger", "Niger", "Niger")) +#' map_data(df_original, "initiative", "country") +#' +#' +#' df_original2 <- data.frame(individuals = c("A", "B", "C", "D"), +#' institutions = c("a b", "a", "a", "b c"), +#' initiatives = c("z", "z", "y z", "y")) +#' map_data(df_original2, "individuals", c("institutions", "initiatives")) +#' +map_data <- function(data, values, node_values) { + # repeat for each in node_values because of different sizes in cols + # separate_longer_delim doesn't repeat for each col, but groups + for (i in node_values){ + data <- data %>% + separate_longer_delim(cols = {{ i }}, " ") + } + + # Step 1: Expand data for the node data + df_expanded <- data %>% + tidyr::pivot_longer(cols = c({{ values }}, {{ node_values }}), + names_to = "group", + values_to = "id") %>% + dplyr::arrange(group) %>% + dplyr::distinct() %>% + dplyr::mutate(id_index = row_number() - 1) + + # Step 2: Create the linked data + df_mapped <- data %>% + dplyr::left_join(df_expanded, by = setNames("id", values)) %>% + dplyr::rename(source = id_index) + + df_mapped <- df_mapped %>% + tidyr::pivot_longer(cols = all_of({{ node_values }}), values_to = "id") %>% + dplyr::distinct() %>% + dplyr::left_join(df_expanded, by = "id") %>% + dplyr::rename(target = id_index) + + df_mapped <- df_mapped %>% + dplyr::select(c(target, source)) + + networkD3::forceNetwork(Links = df_mapped, Nodes = df_expanded, + Source = "source", Target = "target", + NodeID = "id", + Group = "group", + legend = TRUE) +} diff --git a/man/initiative_by.Rd b/man/initiative_by.Rd new file mode 100644 index 0000000..52a998f --- /dev/null +++ b/man/initiative_by.Rd @@ -0,0 +1,86 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/initiative_by.R +\name{initiative_by} +\alias{initiative_by} +\title{"Initiative_by"} +\usage{ +initiative_by( + initiative_data, + by = "pays", + filter_var = NULL, + filter_vals = NULL, + node_size = c("type", "age"), + group = NULL, + font_size = 7, + height = NULL, + width = NULL, + colour_scale = JS("d3.scaleOrdinal(d3.schemeCategory20);"), + font_family = "serif", + link_distance = 50, + link_width = JS("function(d) { return Math.sqrt(d.value); }"), + radius_calculation = "4*Math.sqrt(d.nodesize)+2", + charge = -30, + link_colour = "#666", + opacity = 0.6, + zoom = FALSE, + arrows = FALSE, + bounded = FALSE, + display_labels = 0, + click_action = NULL +) +} +\arguments{ +\item{initiative_data}{a data frame object containing the initiative data} + +\item{by}{} + +\item{filter_var}{} + +\item{filter_vals}{} + +\item{node_size}{character string specifying the a column in the \code{initiative_data} data frame with some value to vary the node radius's with. See also \code{radiusCalculation}.} + +\item{group}{character string specifying the group of each node in the \code{initiative_data} data frame.} + +\item{font_size}{} + +\item{height}{} + +\item{width}{} + +\item{colour_scale}{} + +\item{font_family}{} + +\item{link_distance}{} + +\item{link_width}{} + +\item{radius_calculation}{} + +\item{charge}{} + +\item{link_colour}{} + +\item{opacity}{} + +\item{zoom}{} + +\item{arrows}{} + +\item{bounded}{} + +\item{display_labels}{} + +\item{click_action}{} +} +\value{ +Returns a network graph object +} +\description{ +This function is a wrapper function for \code{networkD3::forceNetwork}. +explain what it does - +} +\examples{ +# todo +} diff --git a/man/map_data.Rd b/man/map_data.Rd new file mode 100644 index 0000000..a6ea62f --- /dev/null +++ b/man/map_data.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/map_data.R +\name{map_data} +\alias{map_data} +\title{Map Data for Network Visualisation} +\usage{ +map_data(data, values, node_values) +} +\arguments{ +\item{data}{The original data frame.} + +\item{values}{The column name representing the primary values to be mapped.} + +\item{node_values}{A character vector specifying the column names representing the node values.} +} +\value{ +A network visualisation generated using the \code{forceNetwork} function. +} +\description{ +This function takes a data frame and generates a network visualization using the \code{forceNetwork} function +from the \code{networkD3} package. It separates and expands the data based on the specified values and node values, +and creates links between them. +} +\examples{ +# An example with simple data +df_original <- data.frame(initiative = c("A", "B", "C", "D"), + country = c("BF", "BF Niger", "Niger", "Niger")) +map_data(df_original, "initiative", "country") + + +df_original2 <- data.frame(individuals = c("A", "B", "C", "D"), + institutions = c("a b", "a", "a", "b c"), + initiatives = c("z", "z", "y z", "y")) +map_data(df_original2, "individuals", c("institutions", "initiatives")) + +}