如何在R中为sankey图准备输入数据?

q9yhzks0  于 2023-07-31  发布在  其他
关注(0)|答案(3)|浏览(137)

我正在尝试在R中生成一个sankey diagram,它也被称为河流图。我看过这个问题Sankey Diagrams in R?,其中列出了产生sankey图的各种软件包。由于我有输入数据,并知道不同的工具/软件包,我可以制作这样的图,但我的euquestion是:如何准备输入数据?
让我们假设我们想要展示用户如何在10天内在各种状态之间迁移,并具有如下所示的开始数据集:

data.frame(userID = 1:100,
                     day1_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day2_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day3_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day4_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day5_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day6_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day7_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day8_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day9_state = sample(letters[1:8], replace = TRUE, size = 100),
                     day10_state = sample(letters[1:8], replace = TRUE, size = 100)
                     ) -> dt

字符串
现在,如果一个人想要用networkD3包创建一个sankey图,那么应该如何将这个dt数据.
这样我们就有了类似于本例输入

library(networkD3)
URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
# Plot
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             units = "TWh", fontSize = 12, nodeWidth = 30)

编辑

我发现了这样的脚本,它在其他情况下准备数据并复制它,所以我认为它现在可能已经关闭:
https://github.com/mi2-warsaw/JakOniGlosowali/blob/master/sankey/sankey.R

jei2mxaa

jei2mxaa1#

我发现了这样的脚本,它在其他情况下准备数据并复制它,所以我认为它现在可能已经关闭:
https://github.com/mi2-warsaw/JakOniGlosowali/blob/master/sankey/sankey.R
然后此代码为问题中提到的数据生成这样的sankey图。

fixtable <- function(...) {
    tab <- table(...)
    if (substr(colnames(tab)[1],1,1) == "_" &
                substr(rownames(tab)[1],1,1) == "_") {
        tab2 <- tab
        colnames(tab2) <- sapply(strsplit(colnames(tab2), split=" "), `[`, 1)
        rownames(tab2) <- sapply(strsplit(rownames(tab2), split=" "), `[`, 1)
        tab2[1,1] <- 0
        # mandat w klubie
        for (par in names(which(tab2[1,] > 0))) {
            delta = min(tab2[par, 1], tab2[1, par])
            tab2[par, par] = tab2[par, par] + delta
            tab2[1, par] = tab2[1, par] - delta
            tab2[par, 1] = tab2[par, 1] - delta
        }
        # przechodzi przez niezalezy
        for (par in names(which(tab2[1,] > 0))) {
            tab2["niez.", par] = tab2["niez.", par] + tab2[1, par]
            tab2[1, par] = 0
        }
        for (par in names(which(tab2[,1] > 0))) {
            tab2[par, "niez."] = tab2[par, "niez."] + tab2[par, 1]
            tab2[par, 1] = 0
        }

        tab[] <- tab2[] 
    }
    tab
}

flow2 <- rbind(
    data.frame(fixtable(z = paste0(dat$day1_state, " day1"), do = paste0(dat$day2_state, " day2"))),
    data.frame(fixtable(z = paste0(dat$day2_state, " day2"), do = paste0(dat$day3_state, " day3"))),
    data.frame(fixtable(z = paste0(dat$day3_state, " day3"), do = paste0(dat$day4_state, " day4"))),
    data.frame(fixtable(z = paste0(dat$day4_state, " day4"), do = paste0(dat$day5_state, " day5"))),
    data.frame(fixtable(z = paste0(dat$day5_state, " day5"), do = paste0(dat$day6_state, " day6"))),
    data.frame(fixtable(z = paste0(dat$day6_state, " day6"), do = paste0(dat$day7_state, " day7"))),
    data.frame(fixtable(z = paste0(dat$day7_state, " day7"), do = paste0(dat$day8_state, " day8"))),
    data.frame(fixtable(z = paste0(dat$day8_state, " day8"), do = paste0(dat$day9_state, " day9"))),
    data.frame(fixtable(z = paste0(dat$day9_state, " day9"), do = paste0(dat$day10_state, " day10"))))

flow2 <- flow2[flow2[,3] > 0,]

nodes2 <- data.frame(name=unique(c(levels(factor(flow2[,1])), levels(factor(flow2[,2])))))
nam2 <- seq_along(nodes2[,1])-1
names(nam2) <- nodes2[,1]

links2 <- data.frame(source = nam2[as.character(flow2[,1])],
                                        target = nam2[as.character(flow2[,2])],
                                        value = flow2[,3])

sankeyNetwork(Links = links, Nodes = nodes,
                            Source = "source", Target = "target",
                            Value = "value", NodeID = "name",
                            fontFamily = "Arial", fontSize = 12, nodeWidth = 40,
                            colourScale = "d3.scale.category20()")

字符串

oknrviil

oknrviil2#

I asked a similar question while ago.我想我最好在这里发布它如何使用tidyverse魔术来完成。

library(ggplot2)
library(ggalluvial)
library(tidyr)
library(dplyr)
library(stringr)

# The actual data preperation happens here
dt_new  <- dt  %>% 
gather(day, state, -userID)  %>% # Long format
mutate(day = str_match(day, "[0-9]+")[,1])  %>% # Get the numbers 
  mutate(day = as.integer(day), # Convert to proper data types
         state = as.factor(state))

字符串
以下是数据dt_new的外观

userID day state
1       1   1     d
2       2   1     d
3       3   1     g
4       4   1     a
5       5   1     a
6       6   1     d
7       7   1     d
8       8   1     b
9       9   1     d
10     10   1     e
...


现在绘制桑基图:

ggplot(dt_new,
       aes(x = day, stratum = state, alluvium = userID, fill = state, label = state)) +
  geom_stratum() +
  geom_text(stat = "stratum") +
  geom_flow()


下面是输出

oyxsuwqo

oyxsuwqo3#

七年了,但仍然值得更新
下面是我写的一些东西,它可以将宽数据转换为sankey兼容。
任何使用它的人都需要做一点编辑,函数中的sankey_pairs列表需要编辑,以便在数据中具有顺序/链式的列对。
这个函数可以创建一个完整的图表,但任何人都可以从中提取他们需要的内容。

sankey_example <-
  expand(
    tibble(),
    x = sample(c('a', 'b', 'c'), size = 10, replace = TRUE),
    y = sample(c('j', 'k', 'l'), size = 10, replace = TRUE),
    z = sample(c('q', 'r', 's'), size = 10, replace = TRUE)
  ) |> 
    mutate(value = round(runif(n = n(), 0, 100)))

create_sankey <- function(data_var){
  
  sankey_pairs <- 
    list(
      c("x",  "y"),
      c("y",  "z"),
    )
  
  custSankey_transformed <- 
    map_dfr(sankey_pairs, function(col_var){
      data_var |>
        group_by(
          source = !!sym(col_var[[1]]),
          target = !!sym(col_var[[2]])
        ) |>
        summarise(value = sum(value, na.rm = TRUE), .groups = 'drop')
    })
  
  Sankey_nodes <- tibble(name = unique(c(Sankey_transformed$source, Sankey_transformed$target)))
  Sankey_transformed$IDsource <- match(Sankey_transformed$source, Sankey_nodes$name)-1
  Sankey_transformed$IDtarget <- match(Sankey_transformed$target, Sankey_nodes$name)-1
  
  out_sankeyNetwork <- 
    sankeyNetwork(
      Links     = Sankey_transformed, 
      Nodes     = Sankey_nodes,
      Source    = "IDsource", 
      Target    = "IDtarget",
      Value     = "value", 
      NodeID    = "name", 
      sinksRight= FALSE,
      fontSize  = 14
    )

  return(out_sankeyNetwork)
  
}

create_sankey(sankey_example)

字符串

相关问题