如何重新排序桑基图的节点从大到小

问题描述 投票:0回答:1

我不确定这是否可能,但我希望重新排序我的第一级节点在我的桑基图中的显示方式,以便它从最大的框到最小的框开始。这是我的数据框的示例

df = structure(list(x = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L), levels = c("Method_Group", "Topic"), class = "factor"), 
    node = c("mobile-receiver", "Behavioural Ecology", "mobile-receiver", 
    "Conservation Measures", "animal-borne-no-receiver", "Other Drivers", 
    "stationary-receiver", "Behavioural Ecology", "stationary-receiver", 
    "Behavioural Ecology", "stationary-receiver", "Reproductive Ecology", 
    "stationary-receiver", "Other Drivers", "stationary-receiver", 
    "Behavioural Ecology", "stationary-receiver", "Methodological", 
    "baited-receiver", "Behavioural Ecology", "baited-receiver", 
    "Methodological", "baited-receiver", "Reproductive Ecology", 
    "baited-receiver"), next_x = structure(c(2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L), levels = c("Method_Group", "Topic"
    ), class = "factor"), next_node = c("Behavioural Ecology", 
    NA, "Conservation Measures", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Reproductive Ecology", NA, 
    "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Landuse Management")), class = c("rowwise_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -25L), groups = structure(list(
    .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
        10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
        21L, 22L, 23L, 24L, 25L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -25L), class = c("tbl_df", 
"tbl", "data.frame")))

这是我正在使用的代码

library(ggsankey)
library(dplyr)
library(ggplot2)

width <- .4

p <- ggplot(df, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 1, node.color = "black", show.legend = FALSE, width = width) +
  theme_void() +
  theme(
    plot.margin = unit(rep(5.5, 4), "pt")
  ) +
  scale_fill_viridis_d()

# Get the data from the flows layer
dat <- layer_data(last_plot(), 1) |>
  filter(x == 2 - width / 2) |>
  distinct(fill, flow_end_ymax, .keep_all = TRUE)

p = p +
  geom_rect(data = dat, aes(
    xmin = x, xmax = x + width,
    ymin = flow_end_ymin, ymax = flow_end_ymax,
    fill = label
  ), inherit.aes = FALSE) +
  geom_sankey_label(size = 5, color = "black", fill = "white") +
  guides(fill = "none")



p

因此,在这个例子中,理想的顺序是固定接收器、诱饵接收器、移动接收器和动物传播的无接收器。请记住,在我的原始数据框中,实际上我有 5 个额外的不同大小的盒子。

我尝试使用

factor()
并手动列出我想要节点的级别,但当我运行代码时没有任何变化。知道是否可以这样做吗?

r ggplot2 sankey-diagram
1个回答
0
投票

由于您没有提供代码,我不知道您尝试过什么

factor
以及为什么这对您不起作用。但基本上你是对的。在 99% 与订单相关的问题中,答案是转换为
factor
,这也适用于您的情况:

library(ggsankey)
library(dplyr, warn = FALSE)
library(ggplot2)

df$node <- factor(
  df$node,
  levels =
    c(
      "stationary-receiver", "baited-receiver",
      "mobile-receiver", "animal-borne-no-receiver",
      "Other Drivers", "Reproductive Ecology",
      "Methodological", "Behavioural Ecology",
      "Conservation Measures"
    )
)

width <- .4

p <- ggplot(df, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 1, node.color = "black", show.legend = FALSE, width = width) +
  theme_void() +
  theme(
    plot.margin = unit(rep(5.5, 4), "pt")
  ) +
  scale_fill_viridis_d()

# Get the data from the flows layer
dat <- layer_data(last_plot(), 1) |>
  filter(x == 2 - width / 2) |>
  distinct(fill, flow_end_ymax, .keep_all = TRUE)

p <- p +
  geom_rect(data = dat, aes(
    xmin = x, xmax = x + width,
    ymin = flow_end_ymin, ymax = flow_end_ymax,
    fill = label
  ), inherit.aes = FALSE) +
  geom_sankey_label(size = 5, color = "black", fill = "white") +
  guides(fill = "none")

p

© www.soinside.com 2019 - 2024. All rights reserved.