使用R冲积层中的冲积层可视化等级变化

我有一个非常基本的df,其中我计算了两个时间戳之间的值的等级变化:

   value rank_A rank_B group
1      A   1     1      A
2      B   2     3      A
3      C   3     2      B
4      D   4     4      B
5      E   5     8      A
6         F   6    5   C
7         G   7    6   C
8         H   8    7   A

(对我而言)让它有些棘手的是在Y轴上绘制值。

ggplot(df_alluvial,aes(y = value,axis1 = rank_A,axis2 = rank_B))+
  geom_alluvium(aes(fill = group),width = 1/12)+
  ...

到目前为止,我可以成功绘制等级变化和组,但它们尚未链接到我的值名称-没有轴名称,并且我不知道如何添加它们。

最后,它应该类似于: https://www.reddit.com/r/GraphicalExcellence/comments/4imh5f/alluvial_diagram_population_size_and_rank_of_uk/

感谢您的建议!

sax110 回答:使用R冲积层中的冲积层可视化等级变化

您的更新使我更清楚了这个问题。

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)

enter image description here

reprex package(v0.3.0)于2019-11-08创建

本文链接:https://www.f2er.com/3139066.html

大家都在问