I made the following network in R:
library(igraph)
library(reshape2)
north_american_cities <- c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
european_cities <- c("London", "Berlin", "Madrid", "Rome", "Paris")
asian_cities <- c("Tokyo", "Delhi", "Shanghai", "Beijing", "Mumbai")
n <- 30
set.seed(123)
na_cities_sample <- sample(north_american_cities, n, replace = TRUE)
eu_cities_sample <- sample(european_cities, n, replace = TRUE)
as_cities_sample <- sample(asian_cities, n, replace = TRUE)
df <- data.frame(NorthAmerica = na_cities_sample,
Europe = eu_cities_sample,
Asia = as_cities_sample,
stringsAsFactors = FALSE)
df <- df[!duplicated(df), ]
edges_df <- data.frame(from = c(df$NorthAmerica, df$Europe),
to = c(df$Europe, df$Asia))
edge_list <- as.matrix(edges_df)
g <- graph_from_edgelist(edge_list, directed = TRUE)
plot(g, vertex.size=10, vertex.label.cex=0.8, edge.arrow.size=0.5)
I then added a single drop down menu for navigation using visNetwork:
library(visNetwork)
nodes_df <- data.frame(id = unique(c(df$NorthAmerica, df$Europe, df$Asia)),
group = c(rep("North America", length(unique(df$NorthAmerica))),
rep("Europe", length(unique(df$Europe))),
rep("Asia", length(unique(df$Asia)))))
edges_df <- data.frame(from = c(df$NorthAmerica, df$Europe),
to = c(df$Europe, df$Asia))
colors <- c('North America' = 'red', 'Europe' = 'green', 'Asia' = 'blue')
visNetwork(nodes_df, edges_df) %>%
visNodes(color = list(background = colors[nodes_df$group])) %>%
visEdges(arrows = 'to') %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visInteraction(navigation = "zoom") %>%
visInteraction(navigation = "drag")
My Question: I am trying to modify the above code to create 3 dropdown menus, that would allow someone to select a north american city and see all possible journeys that can be taken – then to click on a european city to further narrow it down:
I tried searching for examples online, but it seems like visNetwork in R might not allow this option of multiple dropdowns.
- I tried to see if there might be anything possible using javascript events
- I found some options involving R Shiny, e.g. R – state of the art performance of visNetwork vs networkd3 in a Shiny app (2017)
Can someone please show me how I might be able to add these 3 dropdowns?
Thanks!