Skip to content

Commit

Permalink
functionality additions - adding colour option
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Feb 7, 2024
1 parent ffdc248 commit 797dc95
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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,"%>%")
58 changes: 38 additions & 20 deletions R/map_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,68 @@
#' @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){
data <- data %>%
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))
}
31 changes: 31 additions & 0 deletions man/initiative_by.Rd

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

8 changes: 6 additions & 2 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 797dc95

Please sign in to comment.