我正在嘗試創建一個凹凸圖以使用 ggalluvial 可視化時間序列資料。我想使用與該層相關的值“百分比”來標記每個層的中心。我似乎無法讓標簽正確。例如,頂部的值 (12, 14, 8) 應該位于 18-30 磁脈的中心,但由于某種原因,它們在 30-40 磁脈的頂部被發現。有沒有辦法正確標記這個?我還考慮了 geom_sankey_bump。
df <- data.frame(age = c(
'18-30', '18-30', '18-30',
'30-40', '30-40', '30-40',
'40-50', '40-50', '40-50',
'50 ', '50 ', '50 '),
year = c(2012, 2013, 2014,
2012, 2013, 2014,
2012, 2013, 2014,
2012, 2013, 2014),
percent = c(12, 14, 8,
44, 54, 50,
21, 32, 45,
10, 13, 30))
plot <- ggplot(data =df,
aes(x = factor(year),
y = percent,
alluvium = age,
node = factor(year),
stratum = age,
label = percent) )
plot
geom_stratum(aes(stratum = age), decreasing = F, width = 3/4)
geom_alluvium(aes(fill = age), alpha = 1, decreasing = F,
width = 3/4,
knot.prop = T,
curve_type = "linear")
ggfittext::geom_fit_text(stat = "stratum", width = 1/4, min.size = 3)
ggtitle("Marijuana Crime Rate")
theme(plot.title = element_text(hjust = 0.5))
xlab(NULL)
theme_classic()
theme(legend.position = "bottom")

uj5u.com熱心網友回復:
要將標簽放置在正確的位置,您必須對 使用相同的引數stat_stratum,即使用width=3/4和,decreasing=FALSE就像您對 所做的那樣geom_stratum:
library(ggplot2)
library(ggalluvial)
plot <- ggplot(data =df,
aes(x = factor(year),
y = percent,
alluvium = age,
node = factor(year),
stratum = age,
label = percent) )
plot
geom_stratum(aes(stratum = age), decreasing = F, width = 3/4)
geom_alluvium(aes(fill = age), alpha = 1, decreasing = F,
width = 3/4,
knot.prop = T,
curve_type = "linear")
ggfittext::geom_fit_text(stat = "stratum", width = 3/4, decreasing = FALSE, min.size = 3)
ggtitle("Marijuana Crime Rate")
theme(plot.title = element_text(hjust = 0.5))
xlab(NULL)
theme_classic()
theme(legend.position = "bottom")

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471589.html
