diff --git a/NAMESPACE b/NAMESPACE index 476318a..9f6ac92 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export("%>%") +export(map_data) export(individuals_by) export(initiative_by) export(replace_other) 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 ca9a0c5..1c18d1f 100644 --- a/R/initiative_by.R +++ b/R/initiative_by.R @@ -27,7 +27,6 @@ #' @param click_action character string with a JavaScript expression to evaluate when a node is clicked. #' #' @return Returns a network graph object -#' @export #' #' @examples # todo initiative_by <- function(initiative_data, by = "pays", sep = ".", 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 index a4755c6..5e034aa 100644 --- a/man/initiative_by.Rd +++ b/man/initiative_by.Rd @@ -43,37 +43,6 @@ initiative_by( \item{group}{character string specifying the group of each node in the \code{initiative_data} data frame.} -\item{font_size}{numeric font size in pixels for the node text labels.} - -\item{height}{numeric height for the network graph's frame area in pixels.} - -\item{width}{numeric width for the network graph's frame area in pixels.} - -\item{colour_scale}{character string specifying the categorical colour scale for the nodes. See https://github.com/d3/d3/blob/master/API.md#ordinal-scales.} - -\item{font_family}{font family for the node text labels.} - -\item{link_distance}{numeric or character string. Either numberic fixed distance between the links in pixels (actually arbitrary relative to the diagram's size). Or a JavaScript function, possibly to weight by Value. For example: linkDistance = JS("function(d){return d.value * 10}").} - -\item{link_width}{numeric or character string. Can be a numeric fixed width in pixels (arbitrary relative to the diagram's size). Or a JavaScript function, possibly to weight by Value. The default is linkWidth = JS("function(d) { return Math.sqrt(d.value); }").} - -\item{radius_calculation}{character string. A javascript mathematical expression, to weight the radius by Nodesize. The default value is radiusCalculation = JS("Math.sqrt(d.nodesize)+6").} - -\item{charge}{numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value).} - -\item{link_colour}{character vector specifying the colour(s) you want the link lines to be. Multiple formats supported (e.g. hexadecimal).} - -\item{opacity}{numeric value of the proportion opaque you would like the graph elements to be.} - -\item{zoom}{logical value to enable (TRUE) or disable (FALSE) zooming.} - -\item{arrows}{logical value to enable directional link arrows.} - -\item{bounded}{logical value to enable (TRUE) or disable (FALSE) the bounding box limiting the graph's extent. See http://bl.ocks.org/mbostock/1129492.} - -\item{display_labels}{} - -\item{click_action}{character string with a JavaScript expression to evaluate when a node is clicked.} } \value{ Returns a network graph object 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")) + +}