[英]Comma separator for data labels on Alluvial Plot (R ggalluvial)
I am looking to format the value labels with "," separators, particularly on the stratums (bar columns) of Alluvial/ Sankey plot using R ggalluvial. 我正在使用“,”分隔符设置值标签的格式,尤其是在使用R ggalluvial的冲积/桑基图的层(条形列)上。
While similar answers were found on other charts, the same attempt has returned an error (notice the missing value labels and messed up flow connections): 在其他图表上找到相似的答案时 ,相同的尝试返回了一个错误(请注意缺少值标签并弄乱了流量连接):
library(ggplot2)
library(ggalluvial)
library(scales)
vaccinations$freq = vaccinations$freq * 1000
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = comma(freq))) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_text(stat = "stratum", size = 3) +
theme(legend.position = "bottom") +
ggtitle("vaccination survey responses at three points in time")
Warning message:
Removed 12 rows containing missing values (geom_text).
The internals of ggalluvial prevent this from working, as @TobiO suspects. @TobiO怀疑,ggalluvial的内部阻止了它的工作。 Specifically, when a numeric-valued variable is passed to
label
and processed by one of the alluvial stats, it is automatically totaled. 具体来说,当数值变量传递给
label
并由冲积统计数据之一处理时,它将自动求和。 When a character-valued variable is passed to label
, this can't be done. 将字符值的变量传递给
label
,无法完成此操作。 So the formatting must take place after the statistical transformation. 因此,格式化必须在统计转换之后进行。
A solution is provided by ggfittext: The function geom_fit_text()
has a formatter
parameter to which a formatting function can be passed—though the function must be compatible with the type of variable passed to label
! ggfittext提供了一种解决方案:函数
geom_fit_text()
具有一个可以传递格式化功能的formatter
参数,尽管该功能必须与传递给label
的变量类型兼容! Here's an example: 这是一个例子:
library(ggalluvial)
#> Loading required package: ggplot2
library(ggfittext)
library(scales)
data(vaccinations)
vaccinations <- transform(vaccinations, freq = freq * 1000)
ggplot(vaccinations,
aes(x = survey, stratum = response, alluvium = subject,
y = freq,
fill = response, label = freq)) +
scale_x_discrete(expand = c(.1, .1)) +
geom_flow() +
geom_stratum(alpha = .5) +
geom_fit_text(stat = "stratum", size = 10, min.size = 6, formatter = comma) +
theme(legend.position = "bottom") +
ggtitle("vaccination survey responses at three points in time")
Created on 2019-09-04 by the reprex package (v0.2.1) 由reprex软件包 (v0.2.1)创建于2019-09-04
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.