我是使用 ggalluvial 包的新手。我目前正在处理一个捐赠数据集,我想使用冲积图作为媒介来表示该数据集。以下是我正在使用的数据集示例:
donor_ID recip_name donation_amt month_year
<chr> <chr> <dbl> <chr>
1 1 B, P 25 September 2019
2 2 S, B 27 July 2019
3 3 K, A 50 June 2019
4 1 H, K 100 April 2019
5 2 W, E 3 December 2019
6 3 S, B 9 August 2019
7 1 C, J 25 September 2019
8 2 B, J 50 October 2019
9 3 W, E 400 August 2019
10 1 S, B 20 December 2019
此数据集上的 dput() 输出如下:
structure(list(donor_ID = c("1", "2", "3", "1", "2", "3", "1",
"2", "3", "1"), recip_name = c("B, P", "S, B", "K, A", "H, K",
"W, E", "S, B", "C, J", "B, J", "W, E", "S, B"), donation_amt = c(25,
27, 50, 100, 3, 9, 25, 50, 400, 20), month_year = c("September 2019",
"July 2019", "June 2019", "April 2019", "December 2019", "August 2019",
"September 2019", "October 2019", "August 2019", "December 2019"
)), class = "data.frame", row.names = c(NA, -10L))
我希望代表个人捐赠者对谁接受(recip_name
)他们的捐赠可能会因月而异(捐赠者偏好)做出的选择,而donor_ID
代表个人捐赠者。donation_amt
生成的冲积图应显示每月之间的上述变化,其方式也与接受者之间的捐赠总额 ( ) 成正比。以下是我为完成此任务而编写的脚本:
df$recip_name <- as.factor(df$recip_name)
df %>%
filter(transaction_dt < as.Date("2020-01-01")) %>%
select(donor_ID, recip_name, donation_amt, month_year) %>%
ggplot(aes(x = month_year, y = donation_amt, stratum = recip_name,
alluvium = donor_ID, fill = recip_name, label = recip_name)) +
scale_fill_brewer(type = "qual", palette = "Set2") +
geom_flow(stat = "alluvium", color = "darkgray") +
geom_stratum() +
theme_light() +
theme(legend.position = "bottom") +
ggtitle("Donor Preference")
执行此 R 代码后,这是我收到的结果错误:
Error in f(...) :
Data is not in a recognized alluvial form (see `help('alluvial-data')` for details).
我已经对正确设置用于 ggalluvial 的数据的问题进行了研究,但无济于事。如何使用这些数据正确开发所需的冲积图?