首页 > 解决方案 > 如何在 R(ggforce)中的 Sankey 图中调整高度和间距?

问题描述

我有一个用ggforce::geom_parallel_sets.

因为出发地和目的地的类别数量不同(出发地三个,目的地两个),所以它们之间存在高度差异。我希望它们具有相同的高度,这将涉及增加两个目的地类别之间的间隔,但我不知道如何实现这一点。

注意:我确实需要这些休息时间,即根本不能没有休息时间;只需要增加目的地中断处的空间,使其与起点高度相同。另外,我宁愿坚持ggforce(除非你可以休息ggalluvial)。

任何帮助将不胜感激 - 下面的代码并以所需的效果输出。

library(tidyverse)
library(ggforce)

oclassuni <- structure(list(oclass = c("1st", "1st", "2nd", "2nd", "3rd", 
"3rd", "1st", "1st", "2nd", "2nd", "3rd", "3rd"), uni = c("degree", 
"no degree", "degree", "no degree", "degree", "no degree", "degree", 
"no degree", "degree", "no degree", "degree", "no degree"), Freq = c(520, 
719, 1186, 871, 633, 2626, 520, 719, 1186, 871, 633, 2626), Perc = c(42, 
58, 57.7, 42.3, 19.4, 80.6, 42, 58, 57.7, 42.3, 19.4, 80.6), 
    id = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), x = c("oclass", 
    "oclass", "oclass", "oclass", "oclass", "oclass", "uni", 
    "uni", "uni", "uni", "uni", "uni"), y = c("1st", "1st", "2nd", 
    "2nd", "3rd", "3rd", "degree", "no degree", "degree", "no degree", 
    "degree", "no degree")), row.names = c(NA, -12L), groups = structure(list(
    oclass = c("1st", "2nd", "3rd"), .rows = structure(list(c(1L, 
    2L, 7L, 8L), c(3L, 4L, 9L, 10L), c(5L, 6L, 11L, 12L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

ggplot(oclassuni, aes(fct_relevel(x, "uni", "oclass"), id = id, split = y, value = Perc)) +
  geom_parallel_sets(aes(fill = oclass, alpha = uni), axis.width = 3, sep = 0.05) +
  scale_fill_manual(values = c("#5D6BA3", "#E3280B", "#158248")) +
  scale_alpha_manual(values = c(0.8, 0.6)) +
  theme_minimal() +
  theme(axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(), 
        axis.text.x = element_blank(),
        legend.position = "none", 
        plot.title = element_text(hjust=0.5, size=18),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())

在此处输入图像描述

编辑:我已经使用了sep参数并sep = c(0.025, 0.05)给出了以下内容,但是现在原点(!)中有不均匀的空格,并且 sep 不会采用三个值......有什么想法吗?

在此处输入图像描述

标签: rggplot2sankey-diagramggforce

解决方案


推荐阅读