r - 带有百分比和部分填充的桑基/冲积图 R
问题描述
我想修改现有的 sankey 情节ggplot2
,并ggalluvial
使其更具吸引力
我的例子来自https://corybrunson.github.io/ggalluvial/articles/ggalluvial.html
library(ggplot2)
library(ggalluvial)
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = response)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "none") +
ggtitle("vaccination survey responses at three points in time")
由reprex 包(v0.3.0)于 2020 年 10 月 1 日创建
现在,我想更改此图,使其看起来类似于https://sciolisticramblings.wordpress.com/2018/11/23/sankey-charts-the-new-pie-chart/中的图,即 1. 更改绝对相对于相对值(百分比) 2. 添加百分比标签和 3. 应用部分填充(例如“缺失”和“从不”)
我的方法:
我想我可以将轴更改为百分比,例如:scale_y_continuous(label = scales::percent_format(scale = 100))
但是,我不确定第 2 步和第 3 步。
解决方案
这可以像这样实现:
更改为百分比可以通过在您的 df 中添加一个带有调查百分比份额的新列来实现,然后可以将其映射到
y
而不是freq
.要获得不错的百分比标签,您可以使用
scale_y_continuous(label = scales::percent_format())
对于部分填充,您可以映射例如
response %in% c("Missing", "Never")
(fill
提供TRUE
“缺失”和“从不”)并通过以下方式设置填充颜色scale_fill_manual
每个层的百分比可以通过我使用变量并计算的地方添加到标签
label = paste0(..stratum.., "\n", scales::percent(..count.., accuracy = .1))
中。geom_text
..stratum..
..count..
stat_stratum
library(ggplot2)
library(ggalluvial)
library(dplyr)
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
vaccinations <- vaccinations %>%
group_by(survey) %>%
mutate(pct = freq / sum(freq))
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = pct,
fill = response %in% c("Missing", "Never"),
label = response)) +
scale_x_discrete(expand = c(.1, .1)) +
scale_y_continuous(label = scales::percent_format()) +
scale_fill_manual(values = c(`TRUE` = "cadetblue1", `FALSE` = "grey50")) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(aes(label = paste0(..stratum.., "\n", scales::percent(..count.., accuracy = .1))), stat = "stratum", size = 3) +
theme(legend.position = "none") +
ggtitle("vaccination survey responses at three points in time")
推荐阅读
- java - 如何在java中运行程序10000次并找到平均值?
- c++ - create_directory("/MyPath/.MyFolder") 没有创建目录;预计会创建一个隐藏目录
- amazon-web-services - 如何允许桌面应用程序和 Web 应用程序与 RDS SQL Server 实例连接?
- html - AJAX Post 请求答案未到达但显示在网络中
- amazon-web-services - 创建 EMR 集群时出错,EMR 服务角色无效
- python - 如何使它不打印超过 x 出现次数
- mysql - 插入转储 MySQL MariaDB
- java - 了解java代码中最小生成树的一部分
- javascript - 我可以使用 google v8 在 C++ 回调函数中获取 JavaScript 函数的源文本吗?
- python - 在 Dask 中,有没有办法在依赖项可用时处理它们,例如 multiprocessing.imap_unordered?