#|
networkData <- data.frame (
src = snowball_connected$ edges$ from,
target = snowball_connected$ edges$ to,
stringsAsFactors = FALSE
)
nodes <- data.frame (
name = snowball_connected$ nodes$ id,
author = IPBES.R:: abbreviate_authors (snowball_connected$ nodes),
doi = snowball_connected$ nodes$ doi,
nodesize = snowball_connected$ nodes$ cited_by_count / (2024 - snowball_connected$ nodes$ publication_year) * 0.5 ,
paper_type = snowball_connected$ nodes$ paper_type,
stringsAsFactors = FALSE
)
nodes$ id <- 0 : (nrow (nodes) - 1 )
# create a data frame of the edges that uses id 0:9 instead of their names
edges <- networkData |>
left_join (nodes, by = c ("src" = "name" )) |>
select (- src, - author) |>
rename (source = id) |>
left_join (nodes, by = c ("target" = "name" )) |>
select (- target, - author) |>
rename (target = id) |>
mutate (width = 1 )
# make a grouping variable that will match to colours
nodes$ group <- nodes$ paper_type
nodes$ oa_id <- nodes$ name
nodes$ name <- nodes$ author
# control colours with a JS ordinal scale
ColourScale <- 'd3.scaleOrdinal()
.domain([
"Ch 2 KP & Ch 4 2 KP",
"Ch 2 KP", "Ch 2 KP & Ch 4 2", "Ch 2",
"Ch 4 2 KP", "Ch 2 KP & Ch 4 2 KP", "Ch 4 2",
])
.range(["red", "blue", "green", "yellow", "purple", "orange", "pink", "brown"]);'
openDOI <- "window.open(d.doi)"
MyClickScript <- 'alert("You clicked " + d.name + " with the doi " +
d.doi + " of your original R data frame");'
nwg <- forceNetwork (
Links = edges,
Nodes = nodes,
Source = "source" ,
Target = "target" ,
NodeID = "name" ,
Nodesize = "nodesize" ,
Group = "paper_type" ,
Value = "width" ,
opacity = 0.9 ,
zoom = TRUE ,
colourScale = JS (ColourScale),
fontSize = 40 ,
legend = TRUE ,
clickAction = openDOI
)
nwg$ x$ nodes$ doi <- nodes$ doi
networkD3:: saveNetwork (
nwg,
file = file.path ("figures_comp" , "connected.html" ),
selfcontained = TRUE
)
unlink (
list.dirs (path = "figures_comp" , recursive = FALSE ),
recursive = TRUE
)
nwg