使用 ggalluvial 创建冲积图时出现问题

发布于 2025-01-16 20:18:50 字数 999 浏览 6 评论 0原文

我有这个数据集:

type <- c(1, 2, NA, 1, 2, NA)
freq <- c(75, 12, 25, 69, 22, 32)
time <- c("before", "before", "before", "after", "after", "after")

df <- data.frame(type , freq, time )

我需要制作一个像这样的图表(显然有不同的数据):

在此处输入图像描述

我尝试按照此处的指南进行操作: https://cran.r-project.org/web/ packages/ggalluvial/vignettes/ggalluvial.html

使用此代码:

ggplot(modechoice,
       aes(x = time, stratum = type, alluvium = time,
           y = freq,
           fill = type, label = type)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  ggtitle("x")

但我收到错误,我的数据未被识别为冲积层。我做错了什么?

I have this dataset:

type <- c(1, 2, NA, 1, 2, NA)
freq <- c(75, 12, 25, 69, 22, 32)
time <- c("before", "before", "before", "after", "after", "after")

df <- data.frame(type , freq, time )

I need to make a graph like this (with different data obviously):

enter image description here

I tried to follow the guide here:
https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html

Using this code:

ggplot(modechoice,
       aes(x = time, stratum = type, alluvium = time,
           y = freq,
           fill = type, label = type)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  ggtitle("x")

But I get the error that my data is not recognised as an alluvial. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与之呼应 2025-01-23 20:18:50

您应该有第四个变量,它与您的 time 变量链接,这就是我创建一个额外变量作为示例的原因。您可以使用以下代码:

type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "after", "after", "after")
time2 <- c("after", "before", "after", "after", "before", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

输出:

在此处输入图像描述

在 Victor Nielson 评论后编辑:

    type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "before", "before", "before")
time2 <- c("after", "after", "after", "after", "after", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

输出:

在此处输入图像描述

You should have a fourth variable which is linked with your time variable, that's why I created an extra variable as an example. You can use the following code:

type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "after", "after", "after")
time2 <- c("after", "before", "after", "after", "before", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

Output:

enter image description here

Edit after Victor Nielson comment:

    type <- c("1", "2", "NA", "1", "2", "NA")
freq <- c(75, 12, 25, 69, 22, 32)
time1 <- c("before", "before", "before", "before", "before", "before")
time2 <- c("after", "after", "after", "after", "after", "after") 


df <- data.frame(type , freq, time1, time2)

library(tidyverse)
library(ggalluvial)

ggplot(df,
       aes(y = freq, axis1 = time1, axis2 = time2)) +
  geom_alluvium(aes(fill = type), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("time1", "time2"), expand = c(.05, .05)) +
  scale_fill_brewer(type = "qual", palette = "Set1") +
  ggtitle("x")

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文