如何在不改变颜色的情况下将桑基图的节点从大到小重新排序

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

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

viridis scale
的顺序。这是我的数据框的示例

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, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("Method_Group", 
                                                                                        "Topic"), class = "factor"), node = c("Receiver + Mobile", "Behavioural Ecology", 
                                                                                                                              "Receiver + Mobile", "Conservation Measures", "No Receiver + Animal-borne", 
                                                                                                                              "Other Drivers", "Receiver + Stationary", "Behavioural Ecology", 
                                                                                                                              "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
                                                                                                                              "Reproductive Ecology", "Receiver + Stationary", "Other Drivers", 
                                                                                                                              "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
                                                                                                                              "Methodological", "Receiver + Baited", "Behavioural Ecology", 
                                                                                                                              "Receiver + Baited", "Methodological", "Receiver + Baited", "Reproductive Ecology", 
                                                                                                                              "Receiver + Baited", "Landuse Management", "No Receiver + Animal-borne", 
                                                                                                                              "Other Drivers", "No Receiver + Animal-borne", "Behavioural Ecology", 
                                                                                                                              "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
                                                                                                                              "Reproductive Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
                                                                                                                              "No Receiver + Animal-borne", "Fisheries Management", "No Receiver + Animal-borne", 
                                                                                                                              "Behavioural Ecology", "No Receiver + Animal-borne", "Methodological", 
                                                                                                                              "No Receiver + Animal-borne", "Fisheries Management", "Receiver + Stationary", 
                                                                                                                              "Behavioural Ecology", "No Receiver + Animal-borne", "Methodological", 
                                                                                                                              "No Receiver + Animal-borne", "Conservation Measures"), 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, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
                                                                                                                                                                                                           NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
                                                                                                                                                                                                           NA), 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", NA, 
                                  "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
                                  NA, "Reproductive Ecology", NA, "Behavioural Ecology", NA, 
                                  "Fisheries Management", NA, "Behavioural Ecology", NA, "Methodological", 
                                  NA, "Fisheries Management", NA, "Behavioural Ecology", NA, 
                                  "Methodological", NA, "Conservation Measures", NA)), class = c("rowwise_df", 
                                                                                                 "tbl_df", "tbl", "data.frame"), row.names = c(NA, -50L), 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, 26L, 27L, 28L, 29L, 30L, 31L, 
                                                                                                                          32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 
                                                                                                                          43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                 "vctrs_vctr", "list"))), row.names = c(NA, -50L), class = c("tbl_df", 
                                                                                                                                                                                                                                                             "tbl", "data.frame")))


这是我用来从最多到最少重新排序的代码(从最大的盒子到最小的盒子)

df$node <- factor(
  df$node,
  levels =
    c("Receiver + Baited","Receiver + Mobile","Receiver + Stationary",
      "No Receiver + Animal-borne",
      "Behavioural Ecology", 
      "Conservation Measures","Methodological","Other Drivers", 
      "Reproductive Ecology", "Fisheries Management", "Landuse Management"))

这是我用来绘制它的代码

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

当我绘制它时,我得到的东西看起来像这样

如您所见,颜色不遵循

viridis scale
的标准从浅到深格式。有什么想法可以解决这个问题吗

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

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

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.