1

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

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

我想要实现的目标: 移动流程图

我所拥有的: 我的移动流程图示例

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

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())
4

1 回答 1

1

添加您想要的内容非常容易。您只需要映射alphadclass然后设置您想要使用的值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 包于 2021-03-29 创建(v1.0.0)

于 2021-03-29T14:27:50.750 回答