您的更新使我更清楚了这个问题。
y
参数应为数字值,并且数据应为“长”格式。我不确定如何更改您的数据以满足这些要求。因此,在此示例中,我将创建一些新数据。我试图使数据类似于您链接到的绘图中的数据。
标签和阶层是指城市名称。您可以使用geom_text
来标记层。
# Load libraries
library(tidyverse)
library(ggalluvial)
# Create some data
df_alluvial <- tibble(
city = rep(c("London","Birmingham","Manchester"),4),year = rep(c(1901,1911,1921,1931),each = 3),size = c(0,10,100,15,20,30,25,100))
# Notice the data is in long-format
df_alluvial
#> # A tibble: 12 x 3
#> city year size
#> <chr> <dbl> <dbl>
#> 1 London 1901 0
#> 2 Birmingham 1901 10
#> 3 Manchester 1901 100
#> 4 London 1911 10
#> 5 Birmingham 1911 15
#> 6 Manchester 1911 100
#> 7 London 1921 15
#> 8 Birmingham 1921 20
#> 9 Manchester 1921 100
#> 10 London 1931 30
#> 11 Birmingham 1931 25
#> 12 Manchester 1931 100
ggplot(df_alluvial,aes(x = as.factor(year),stratum = city,alluvium = city,y = size,fill = city,label = city))+
geom_stratum(alpha = .5)+
geom_alluvium()+
geom_text(stat = "stratum",size = 3)
如果要根据城市的大小对城市进行排序,可以将decreasing = TRUE
添加到图中的所有图层。
ggplot(df_alluvial,label = city))+
geom_stratum(alpha = .5,decreasing = TRUE)+
geom_alluvium(decreasing = TRUE)+
geom_text(stat = "stratum",size = 3,decreasing = TRUE)
由reprex package(v0.3.0)于2019-11-08创建
本文链接:https://www.f2er.com/3139066.html