how to make appropriate sankey diagram by using below data

year urban cropland pasture forest scrubland no vegetation water
1900 1086 11088 1094 24623 14774 167 844
1924 1142 11242 1256 23939 15091 162 844
1948 1225 12451 1460 22430 15105 161 844
1972 1986 16278 3789 20808 9855 116 844
1996 2794 16165 3792 18604 11350 127 844
2019 3194 19713 3823 17742 8266 94 844

i want to show difference from by using sankey digram year by year 1900,1924,1948,1972,1996,2019. can you help me someone?

my code but it doesnt work

ibrary(networkD3)

Data

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)
)

Create Sankey Diagram

sankey_data <- data[, c("Year", "Urban", "Cropland", "Pasture", "Forest", "Scrubland", "No_Vegetation", "Water")]

Extract unique node names

nodes <- data.frame(name = unique(c(as.character(sankey_data$Year), names(sankey_data)[-1])))

sankey_graph <- sankeyNetwork(
Links = sankey_data,
Nodes = nodes,
Source = "Year",
Target = names(sankey_data)[-1],
units = "TWh",
fontSize = 12,
nodeWidth = 30
)

Display the Sankey Diagram

print(sankey_graph)

something like this?

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("^[0-9]{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"
)

1 Like

no it should be like that Sankey diagram – from Data to Viz (data-to-viz.com) can you make this for me

Sorry, I don’t understand what you want. I don’t see a sankey on that page that is organized by year or anything analogous.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.