gpt4 book ai didi

r - R 中带有百分比和部分填充的桑基/冲积图

转载 作者:行者123 更新时间:2023-12-04 01:13:16 27 4
gpt4 key购买 nike

我想使用 ggplot2ggalluvial 修改现有的桑基图,使其更具吸引力

我的例子来自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 package 创建于 2020-10-01 (v0.3.0)

现在,我想更改此图,使其看起来类似于 https://sciolisticramblings.wordpress.com/2018/11/23/sankey-charts-the-new-pie-chart/ 中的图,即 1. 将绝对值更改为相对值(百分比) 2. 添加百分比标签和 3. 应用部分填充(例如“缺失”和“从不”) enter image description here

我的方法:我想我可以将轴更改为百分比,例如:scale_y_continuous(label = scales::percent_format(scale = 100))但是,我不确定第 2 步和第 3 步。

最佳答案

这可以这样实现:

  1. 更改为百分比可以通过向您的 df 添加一个新列来实现,其中包含调查的百分比份额,然后可以将其映射到 y 而不是 freq.

  2. 要获得漂亮的百分比标签,您可以使用 scale_y_continuous(label = scales::percent_format())

  3. 对于部分填充,您可以映射,例如response %in% c("Missing", "Never")fill(这为“Missing”和“Never”提供 TRUE)并通过 scale_fill_manual

    设置填充颜色
  4. 可以通过 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")

关于r - R 中带有百分比和部分填充的桑基/冲积图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64146971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com