使用 ggalluvial 包将标签数据添加到桑基绘图轴

Adding label data to sankey plot axis with ggalluvial Package

如何将白色框中的标签名称添加到数据框中的 ggalluvial sankey 图上的三个轴中的每一个? (见图)

“国家/地区”列包含多个国家/地区的观察结果。在下面的示例中,我关注的是带有“IE”标签的爱尔兰。如何使用下面的 ggplot 代码获取国家名称的数据框?我还将为“疫苗”和“目标组”名称执行此任务,因此如果解决方案也可以包含这些列,我将不胜感激。谢谢

Dataframe 代码和 sankey 颜色 - 数据来自欧盟 ECDC

data <- read.csv("https://opendata.ecdc.europa.eu/covid19/vaccine_tracker/csv/data.csv", na.strings = "", fileEncoding = "UTF-8-BOM")
colnames(data)[2] <- "Country"
TestData <- data
Ireland <- subset(TestData, Country == "IE")

Combined <- rbind(Ireland ) #removed other countries for ease of reading

Col1 <- "slateblue2"
Col2 <- "chartreuse2"
Col3 <- "tomato2"
Col4 <- "orange2"
Col5 <- "plum3"
Col6 <- "grey43"
Col7 <- "deeppink"

alpha <- 0.2

这是 sankey 代码 - 这是我没有正确地将标签应用到相关轴的地方

ggplot(Combined,
       aes(weight = FirstDose, axis1 = Vaccine, axis2 = Country, axis3 = TargetGroup)) +
  geom_alluvium(aes(fill = Vaccine, color = Vaccine), 
                width = 0.1, alpha = alpha, knot.pos = 0.4) +
  geom_stratum(width = 0.2, color = "black") +
  geom_label(stat = "stratum", label.strata = TRUE) +
  scale_x_continuous(breaks = 1:3, labels = c("Vaccine", "Country", "TargetGroup"))     +
  scale_fill_manual(values  = c(Col1, Col2, Col3, Col4, Col5, Col6, Col7)) +
  scale_color_manual(values = c(Col1, Col2, Col3, Col4, Col5, Col6, Col7)) +
  ggtitle("ECDC Vaccination data") +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 12, face = "bold")
  )

这是图像 - 您可以看到轴框缺少标签

搞清楚了 - 完整的代码块在这里。我希望这对某人有所帮助

ggplot(Combined,
       aes(weight = FirstDose, axis1 = Vaccine, axis2 = Country, axis3 = TargetGroup)) +
  geom_alluvium(aes(fill = Vaccine, color = Vaccine), 
                width = 0.1, alpha = alpha, knot.pos = 0.4) +
  geom_stratum(width = 0.2, color = "black") +
  geom_text(stat = "stratum", aes(label = after_stat(deposit)),
            color = "black") + 
  geom_label(stat = "stratum", aes(label = after_stat(stratum)), min.y = 200) + 
  scale_x_continuous(breaks = 1:3, labels = c("Vaccine", "Country", "TargetGroup"))     +
  scale_fill_manual(values  = c(Col1, Col2, Col3, Col4, Col5, Col6, Col7)) +
  scale_color_manual(values = c(Col1, Col2, Col3, Col4, Col5, Col6, Col7)) +
  ggtitle("ECDC Vaccination data") +
  theme_minimal() +
  theme(legend.position = "none",     panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),     axis.text.y = element_blank(),       
        axis.text.x = element_text(size = 12, face = "bold")
  )

通过在 geom_stratum() 参数之后添加这两行(我还没有完全理解它们)。标签在我想要的地方。还不好看

  geom_text(stat = "stratum", aes(label = after_stat(deposit)),
            color = "black") + 
  geom_label(stat = "stratum", aes(label = after_stat(stratum)), min.y = 200) + 

解决方案

答案是在这个博客上找到的 post http://corybrunson.github.io/ggalluvial/reference/stat_stratum.html