gpt4 book ai didi

r - Plotly Sankey 微调;节点沿 x 轴对齐,下降

转载 作者:行者123 更新时间:2023-12-04 10:28:39 24 4
gpt4 key购买 nike

下图与我要找的很接近,但是我想知道以下是否可行:

  • 节点左对齐而不是
    沿 x 轴对齐?,例如,只有 2 个节点的流
    将在 x 轴的一半处完成,而不是在 x 最大处(在我的非玩具桑基图中,这是左对齐的,但是,我无法计算出差异)
  • 仅删除节点上的悬停文本(而不是链接上)。我尝试了“标签”、“文本”、“值”、“百分比”、“名称”与“+”或“全部”或“无”或“跳过”的各种组合,但这些似乎都没有有区别。
  • 例如,使用 NA 处理下车,我​​不想看到从 SA 到 Drop(蓝色节点)的链接,但确实希望看到 x=-1 处的绿色条以显示一个人去了 SA他们的第一个假期,还没有过另一个假期。 (如果我保留 source=SA 和 target=NA,图表是空白的)。我建议的解决方法是将 DROP 节点和 SA-DROP 链接着色为白色...

  • 用蓝色的所需变化对图像进行了注释。
    Annotated Sankey Diagram
    require(dplyr); require(plotly); require(RColorBrewer); require(stringr)

    # Summarise flow data
    dat <- data.frame(customer = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5),
    holiday_loc = c("SA", "SA", "AB", "SA", "SA", "SA", "SA", "AB", "AB", "SA", "SA", "SA")) %>%
    group_by(customer) %>%
    mutate(holiday_num = seq_along(customer),
    source=paste0(holiday_loc, '_', holiday_num),
    target = lead(source),
    last_hol = ifelse(holiday_num == n(), 'Y', 'N')) %>%
    filter(last_hol== 'N'| holiday_num == 1) %>%
    select(-last_hol)

    sank_links <- dat %>%
    group_by(source, target) %>%
    summarise(n=n()) %>%
    mutate(target=ifelse(is.na(target), "DROP", target)) # is there another option here?

    # obtain colours for nodes
    f <- function(pal) brewer.pal(brewer.pal.info[pal, "maxcolors"], pal)
    cols <- f("Set1")

    # set up nodes
    sank_nodes <- data.frame(
    name = factor(sort(unique(c(as.character(sank_links$source),
    as.character(sank_links$target)))))
    ) %>%
    mutate(label=sub("_[0-9]$", "", name),
    # for some unknown reason, plotly allows only three labels to be the same
    label_pad=sub("_[1-3]$", "", name),
    label_pad=sub("_[4-6]$", " ", label_pad)) %>%
    arrange(label) %>%
    mutate(color = cols[cumsum(1-duplicated(label))])

    # update links to get index of node and name (without holiday_num)
    sank_links <- sank_links %>%
    mutate(source_num = match(source, sank_nodes$name) -1 ,
    source_name = str_replace(source, "_[0-9]$", ""),
    target_num = match(target, sank_nodes$name) - 1,
    target_name = str_replace(target, "_[0-9]$", ""))


    # diagram
    p <- plot_ly(
    type = "sankey",
    domain = c(
    x = c(0,1),
    y = c(0,1)
    ),
    orientation = "h",
    valueformat = ".0f",
    valuesuffix = "Customers",
    arrangement="fixed",


    node = list(
    label = sank_nodes$label_pad,
    color = sank_nodes$color,
    pad = 15,
    thickness = 15,
    line = list(
    color = "black",
    width = 0.5
    )
    ),

    link = list(
    source = sank_links$source_num,
    target = sank_links$target_num,
    value = sank_links$n
    )
    ) %>%
    layout(
    title = "",
    font = list(
    size = 10
    ),
    xaxis = list(showgrid = F, zeroline = F),
    yaxis = list(showgrid = F, zeroline = F)
    )

    p

    编辑 :我最初不知道如何使用与节点对应的中断来标记 x 轴并为 x 轴提供标题;代码如下:
        %>% 
    layout(
    title = "",
    font = list(
    size = 10
    ),
    xaxis = list(showgrid = F, zeroline = F, title="Holiday Number", tickvals=-1:4, ticktext=1:6),
    yaxis = list(showgrid = F, zeroline = F, showticklabels=FALSE)
    )

    来源: https://plot.ly/r/reference/#layout-xaxis-tickformat

    最佳答案

    您无法更改 Plotly 中节点的位置,但如果您将排列从“固定”更改为“自由形式”,您可以在渲染图表后手动将节点移动到您想要的任何位置。但是,每次呈现图表时,这都必须由用户手动完成。目前无法在 Plotly 脚本中对节点进行排序。

    关于r - Plotly Sankey 微调;节点沿 x 轴对齐,下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48807397/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com