diff --git a/NAMESPACE b/NAMESPACE index 9f6ac92..4a3844b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,7 @@ # Generated by roxygen2: do not edit by hand export("%>%") -export(map_data) export(individuals_by) -export(initiative_by) +export(map_data) export(replace_other) importFrom(magrittr,"%>%") diff --git a/R/map_data.R b/R/map_data.R index 657a992..020cb9a 100644 --- a/R/map_data.R +++ b/R/map_data.R @@ -14,16 +14,20 @@ #' @examples #' # An example with simple data #' df_original <- data.frame(initiative = c("A", "B", "C", "D"), -#' country = c("BF", "BF Niger", "Niger", "Niger")) +#' country = c("BF", "BF Niger", "Niger", "Niger"), +#' ex = c("a b", "a b", "a", "b")) #' map_data(df_original, "initiative", "country") +#' map_data(df_original, "initiative", "country", "ex") +#' map_data(df_original, "initiative", c("country", "ex"), "ex") #' #' #' 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(df_original2, "individuals", c("institutions", "initiatives"), "institutions") #' -map_data <- function(data, values, node_values) { +map_data <- function(data, values, node_values, colour_values = NULL) { # 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){ @@ -31,33 +35,47 @@ map_data <- function(data, values, node_values) { separate_longer_delim(cols = {{ i }}, " ") } - # Step 1: Expand data for the node data + # Step 1: Expand data for df_output_1 df_expanded <- data %>% - tidyr::pivot_longer(cols = c({{ values }}, {{ node_values }}), - names_to = "group", - values_to = "id") %>% - dplyr::select(c(id, group)) %>% - dplyr::arrange(group) %>% - dplyr::distinct() %>% - dplyr::mutate(id_index = row_number() - 1) + pivot_longer(cols = c({{ values }}, {{ node_values }}), + names_to = "group", + values_to = "id") %>% + dplyr::select(c("id", "group")) %>% + arrange(group) - # Step 2: Create the linked data + if (is.null(colour_values)){ + df_expanded <- df_expanded %>% + dplyr::distinct() %>% + dplyr::mutate(id_index = row_number() - 1) %>% + rename(group_type = group) + } else { + data_1 <- data %>% + dplyr::select(c(id = {{ values }}, group_type = {{ colour_values }})) + df_expanded <- df_expanded %>% + left_join(data_1, by = "id") %>% + #rename(group_type = {{ colour_values }}) %>% + dplyr::distinct() %>% + dplyr::mutate(id_index = row_number() - 1) %>% + dplyr::mutate(group_type = ifelse(is.na(group_type), group, group_type)) + } + + # Step 2: Create df_output_2 df_mapped <- data %>% dplyr::left_join(df_expanded, by = setNames("id", values)) %>% - dplyr::rename(source = id_index) + rename(source = id_index) df_mapped <- df_mapped %>% - tidyr::pivot_longer(cols = all_of({{ node_values }}), values_to = "id") %>% - dplyr::distinct() %>% + pivot_longer(cols = all_of({{ node_values }}), values_to = "id") %>% + distinct %>% dplyr::left_join(df_expanded, by = "id") %>% - dplyr::rename(target = id_index) + 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) + return(forceNetwork(Links = df_mapped, Nodes = df_expanded, + Source = "source", Target = "target", + NodeID = "id", + Group = "group_type", + legend = TRUE)) } diff --git a/man/initiative_by.Rd b/man/initiative_by.Rd index 5e034aa..a4755c6 100644 --- a/man/initiative_by.Rd +++ b/man/initiative_by.Rd @@ -43,6 +43,37 @@ 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 index a6ea62f..102fdb0 100644 --- a/man/map_data.Rd +++ b/man/map_data.Rd @@ -4,7 +4,7 @@ \alias{map_data} \title{Map Data for Network Visualisation} \usage{ -map_data(data, values, node_values) +map_data(data, values, node_values, colour_values = NULL) } \arguments{ \item{data}{The original data frame.} @@ -24,13 +24,17 @@ 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")) + country = c("BF", "BF Niger", "Niger", "Niger"), + ex = c("a b", "a b", "a", "b")) map_data(df_original, "initiative", "country") +map_data(df_original, "initiative", "country", "ex") +map_data(df_original, "initiative", c("country", "ex"), "ex") 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(df_original2, "individuals", c("institutions", "initiatives"), "institutions") }