Skip to content

Commit

Permalink
Adding map_data R function
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Feb 7, 2024
1 parent c79499f commit 6a934b3
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(map_data)
importFrom(magrittr,"%>%")
1 change: 1 addition & 0 deletions R/Functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"){
Expand Down
1 change: 0 additions & 1 deletion R/initiative_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions R/map_data.R
Original file line number Diff line number Diff line change
@@ -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)
}
86 changes: 86 additions & 0 deletions man/initiative_by.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions man/map_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a934b3

Please sign in to comment.