library(dplyr)
library(tidyr)
library(networkD3)
data <- data.frame(
Year = c(1900, 1920, 1940, 1960, 1980, 2000, 2019),
Urban = c(1086, 1126, 1196, 1501, 2290, 2857, 3194),
Cropland = c(11088, 11168, 12001, 13091, 16354, 16371, 19713),
Pasture = c(1094, 1234, 1379, 1563, 3797, 3792, 3823),
Forest = c(24623, 24051, 22970, 21633, 20247, 17989, 17742),
Scrubland = c(14774, 15090, 15125, 14883, 10036, 11693, 8266),
No_Vegetation = c(167, 163, 161, 161, 108, 130, 94),
Water = c(844, 844, 844, 844, 844, 844, 844)
)
links <-
data %>%
pivot_longer(-Year) %>%
unite("source", Year, name, remove = FALSE) %>%
mutate(target = lead(source, order_by = Year), .by = name) %>%
filter(!is.na(target))
nodes <- data.frame(name = unique(c(links$source, links$target)))
nodes$group <- sub("[1]{4}_", "", nodes$name)
links$source <- match(links$source, nodes$name) - 1
links$target <- match(links$target, nodes$name) - 1
sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
NodeGroup = "group"
)
0-9 ↩︎