sankey/alluvial R 中带有百分比和部分填充的图表
sankey/alluvial diagram with percentage and partial fill in R
我想使用 ggplot2
和 ggalluvial
修改现有的 sankey 图,使其更具吸引力
我的例子来自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 (v0.3.0)
于 2020-10-01 创建
现在,我想更改此图,使其看起来与 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")
on fill
(“Missing”和“Never”给出 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")
我想使用 ggplot2
和 ggalluvial
修改现有的 sankey 图,使其更具吸引力
我的例子来自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 (v0.3.0)
于 2020-10-01 创建现在,我想更改此图,使其看起来与 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")
onfill
(“Missing”和“Never”给出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")