代码之家  ›  专栏  ›  技术社区  ›  Daniela Rodrigues

R中的Sankey图:如何改变与每个节点相关的各个截面的高度(Y)?

  •  2
  • Daniela Rodrigues  · 技术社区  · 7 年前

    如何更改每个节/节点的高度 Sankey diagram ? 我想创建如下图1所示的内容,其中“性别”部分较小,然后“原因”部分较大,然后“年龄”部分较小:

    Image 1

    Image 2

    代码:

    library(ggplot2)
    library(ggalluvial)
    library(RColorBrewer)
    
    dfs <- dftest[ , c("Hospital", "Paciente", "Terapia", "Unit")]
    alpha <- 1
    getPalette <- colorRampPalette(brewer.pal(12, "Set3"))
    colourCount <- length(unique(dfs$Hospital))
    ggplot(dfs,
           aes(axis1 = Hospital, axis2 = Paciente, axis3=Terapia)) +
      geom_alluvium(aes(fill = Hospital), 
                    width = 1/12, alpha = alpha, knot.pos = 0.5) +
      geom_stratum(width = 1/20) +
      scale_x_continuous(breaks = 1:3, labels = c("Hospital", "Patient", "Therapy")) +
      scale_fill_manual(values = getPalette(colourCount)) +
      ggtitle("Teste") +
      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"))
    

    我有3个节段/节点,中心(24个独特),病人(750个独特)和治疗(10个独特)。所以,我想我可以创建一个类似于图1的sankey图。在下面你可以找到 dput(dfs) 对于一个虚构的数据集,因为我的数据集太大(750人)不能包含在这里。

    dput(dfs)
    structure(list(Hospital = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", 
    "2", "3", "4", "5"), class = "factor"), Paciente = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
    5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
    ), .Label = c("21", "22", "23", "24", "25", "26", "27"), class = "factor"), 
        Terapia = structure(c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
        3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 
        1L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
        3L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 
        1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("Adalimumab", 
        "Etanercept", "Infliximab", "Rituximab"), class = "factor"), 
        Unit = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = "data.frame", row.names = c(NA, 
    -65L))
    

    有人能给我建议吗?

    0 回复  |  直到 7 年前
        1
  •  2
  •   Z.Lin    7 年前

    我认为 ggalluvial 包装的几何不是为自由浮动部分设计的。然而,正如它的创建者在包的小插曲中指出的那样 ggforce 如果您想要的是以下外观,则package具有类似的功能:

    plot

    library(ggforce)
    
    # transform dataframe into appropriate format
    dfs2 <- gather_set_data(dfs, 1:3)
    
    # define axis-width / sep parameters once here, to be used by
    # each geom layer in the plot
    aw <- 0.1
    sp <- 0.1
    
    ggplot(dfs2, 
           aes(x = x, id = id, split = y, value = Unit)) +
      geom_parallel_sets(aes(fill = Hospital), alpha = 0.3, 
                         axis.width = aw, sep = sp) +
      geom_parallel_sets_axes(axis.width = aw, sep = sp) +
      geom_parallel_sets_labels(colour = "white", 
                                angle = 0, size = 3,
                                axis.width = aw, sep = sp) +
      theme_minimal()
    

    下面是一些具有不同参数值的演示:

    demonstrations