【问题标题】:Additional colour transparency categories in ggalluvialggalluvial 中的其他颜色透明度类别
【发布时间】:2021-06-25 12:41:53
【问题描述】:

我正在尝试重现移动流程图,但我真的不知道如何根据 axis2 类别向填充参数添加额外的颜色透明度。或者这是否是解决这个问题的方法!

任何建议将不胜感激,谢谢!

我想要实现的目标: Mobility flow diagram

我有什么: My mobility flow diagram example

我的移动流程图示例的代码:

library(ggplot2)
library(ggalluvial)

oclass <- c("1st", "1st", "1st", "2nd", "2nd", "2nd", "3rd", "3rd", "3rd")
dclass <- c("1st", "2nd", "3rd", "1st", "2nd", "3rd", "1st", "2nd", "3rd")
Freq  <- c(700, 200, 100, 200, 600, 200, 50, 250, 700)

odclass <- data.frame(oclass, dclass, Freq)

ggplot(odclass, aes(y = Freq, axis1 = oclass, axis2 = dclass)) + 
       geom_alluvium(aes(fill = oclass), width = 1/6, reverse = TRUE) +
       geom_stratum(width = 1/6, alpha = 0, reverse = TRUE, color = "black") +
       geom_text(aes(label = after_stat(stratum)), stat = "stratum", reverse = TRUE, size=5) +
       scale_fill_manual(values = c("darkcyan", "darkgoldenrod2", "mediumorchid")) +
       theme_minimal() +
       theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", 
             plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())

【问题讨论】:

  • 根据您要强调的内容,我认为这不是表示数据的好方法,但这是另一个论坛的问题。在统计和数据方面,StackOverflow 更多的是关于“如何”而不是“如果/为什么”。有关“如何”的答案,请参见下文。

标签: r ggplot2 sankey-diagram ggalluvial


【解决方案1】:

添加所需内容非常容易。您只需将alpha 映射到dclass,然后使用scale_alpha_manual() 设置您想要的值。

library(tidyverse)
library(ggalluvial)
#> Warning: package 'ggalluvial' was built under R version 4.0.4

oclass <- c("1st", "1st", "1st", "2nd", "2nd", "2nd", "3rd", "3rd", "3rd")
dclass <- c("1st", "2nd", "3rd", "1st", "2nd", "3rd", "1st", "2nd", "3rd")
Freq  <- c(700, 200, 100, 200, 600, 200, 50, 250, 700)

odclass <- data.frame(oclass, dclass, Freq)

ggplot(odclass, aes(y = Freq, axis1 = oclass, axis2 = dclass)) + 
  geom_alluvium(aes(fill = oclass, alpha = dclass), width = 1/6, reverse = TRUE) +
  geom_stratum(width = 1/6, alpha = 0, reverse = TRUE, color = "black") +
  geom_text(aes(label = after_stat(stratum)), stat = "stratum", reverse = TRUE, size=5) +
  scale_fill_manual(values = c("darkcyan", "darkgoldenrod2", "mediumorchid")) +
  scale_alpha_manual(values = c(0.9, 0.7, 0.5)) +
  theme_minimal() +
  theme(axis.title.y = element_blank(), axis.text.y= element_blank(), legend.position = "none", 
        plot.title = element_text(hjust=0.5, size=18), axis.text.x = element_blank())
#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.
#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

#> Warning in to_lodes_form(data = data, axes = axis_ind, discern =
#> params$discern): Some strata appear at multiple axes.

reprex package (v1.0.0) 于 2021-03-29 创建

【讨论】:

  • 太棒了,谢谢!我没想到,但现在我知道了。
猜你喜欢
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2012-07-11
  • 2011-10-06
  • 2016-04-24
相关资源
最近更新 更多