如何修复GGSankey错误:unimplemented type 'list' in 'orderVector1'

jdzmm42g  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(89)

几个月来,我一直在使用相同的代码来使用ggsankey创建一个图形。但是今天我运行代码,出于某种原因,我得到了一个错误消息。我不记得我是否改变了框架中的任何东西,但我不认为我做了。
下面是一个示例的框架:

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 = list(c("mobile", "receiver"), "Behavioural Ecology", 
        c("mobile", "receiver"), "Conservation Measures", c("animal", 
        "borne", "no", "receiver"), "Other Drivers", c("stationary", 
        "receiver"), "Behavioural Ecology", c("stationary", "receiver"
        ), "Behavioural Ecology", c("stationary", "receiver"), 
        "Reproductive Ecology", c("stationary", "receiver"), 
        "Other Drivers", c("stationary", "receiver"), "Behavioural Ecology", 
        c("stationary", "receiver"), "Methodological", c("baited", 
        "receiver"), "Behavioural Ecology", c("baited", "receiver"
        ), "Methodological", c("baited", "receiver"), "Reproductive Ecology", 
        c("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 = list("Behavioural Ecology", 
        NULL, "Conservation Measures", NULL, "Other Drivers", 
        NULL, "Behavioural Ecology", NULL, "Behavioural Ecology", 
        NULL, "Reproductive Ecology", NULL, "Other Drivers", 
        NULL, "Behavioural Ecology", NULL, "Methodological", 
        NULL, "Behavioural Ecology", NULL, "Methodological", 
        NULL, "Reproductive Ecology", NULL, "Landuse Management")), row.names = c(NA, 
-25L), class = c("tbl_df", "tbl", "data.frame"))

字符串
这是我运行的代码,使ggsankey情节-我有额外的代码,使其格式正确,但这是我得到的错误。

library("viridis")
library(ggplot2)
library(dplyr)
library("ggalluvial")
library(stringr)
library("ggh4x")
library(forcats)
library(ggpubr)
library(tidyr)
library("tokenizers")
library(ggsankey)
remotes::install_github("davidsjoberg/ggsankey")
#sorry for providing all the libraries, I can't remember which ones are needed for this code, and I can't run it to find out

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=0.40) +
  geom_sankey_label(size = 5, color = "black", fill= "white") +
  theme_bw() +
  theme(axis.title = element_blank()
        , axis.text.y = element_blank()
        , axis.text.x = element_blank()
        , axis.ticks = element_blank()  
        , panel.grid = element_blank())+
  #scale_fill_viridis_d(option = "inferno")
  scale_fill_viridis(discrete=TRUE)


正如我上面提到的,它可以很好地工作。现在,当我运行代码时,我得到这个错误消息:

Error in `geom_sankey()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `order()`:
! unimplemented type 'list' in 'orderVector1'
Run `rlang::last_trace()` to see where the error occurred.


当我在网上看到别人提到这个问题是一个列表格式,所以我尝试运行这段代码之前,我使用了我提供的ggplot代码中的框架

df <- as.data.frame(lapply(df, unlist))


但这只会给我更多的错误。有什么想法如何解决这个问题吗?
作为参考,在我使用了我的附加代码以更好地格式化它之后,最终产品看起来像这样。

1sbrub3j

1sbrub3j1#

这些修改能达到预期的效果吗?
1.将node元素转换为字符串
1.将next_nodeNULL值替换为NA s
这就产生了一个桑基图。

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

df1 <- 
  df |> 
  dplyr::rowwise() |> 
  dplyr::mutate(node = paste(unlist(node), collapse = "-"),
                next_node = ifelse(is.null(next_node), NA, next_node))
  
ggplot(df1, 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=0.40) +
  geom_sankey_label(size = 5, color = "black", fill= "white") +
  theme_bw() +
  theme(axis.title = element_blank()
        , axis.text.y = element_blank()
        , axis.text.x = element_blank()
        , axis.ticks = element_blank()
        , panel.grid = element_blank())+
  scale_fill_viridis(discrete=TRUE)

字符串
x1c 0d1x的数据
创建于2023-12-21使用reprex v2.0.2

相关问题