R Plotly Sunburst 图表颜色排序问题

问题描述

我正在尝试在 R Plotly 中制作一个“飞镖”式的旭日图。有 4 个环 r 向外辐射。每个环中有 6 个域 d。每个域内有 3 个指标 i。 每个部分都有一个唯一的 ID,例如。环 4,域 1,指标 3 = r4.d1.i3

每个环具有相同的基色,但为了轻松区分域,每隔一个域的透明度为 50%。

我的问题是域 1 和域 4 中颜色的指示器排序混乱(见下图)。我不知道为什么会这样。

## Libraries
library(plotly)
library(tidyverse)

## Index objects
color_index <- data.frame(
        col = c("#409e09","#409e0980","#409e09","#1ff050","#1ff05080","#e0c80b","#e0c80b80","#757070","#c7121280","#c71212","#c7121280"),match = c("r1.d1","r1.d2","r1.d3","r1.d4","r1.d5","r1.d6","r2.d1","r2.d2","r2.d3","r2.d4","r2.d5","r2.d6","r3.d1","r3.d2","r3.d3","r3.d4","r3.d5","r3.d6","r4.d1","r4.d2","r4.d3","r4.d4","r4.d5","r4.d6"
                  ))
ring_index <- seq(from = 1,to = 4,by = 1)
domain_index <- seq(from = 1,to = 6,by = 1)
indicator_index <- seq(from = 1,to = 3,by = 1)
indices <- expand.grid(stringsAsFactors = FALSE,r = ring_index,d = domain_index,i = indicator_index)

## Data frame set up for plot_ly
# Put correct colors and parents onto each indicator based on domain
df_indices <- indices %>% 
        mutate(id = paste0("r",indices$r,".d",indices$d,".i",indices$i),match = paste0("r",indices$d)) %>% 
        left_join(color_index,by = c("match" = "match")) %>% 
        mutate(parent = if_else(r == 1,NA_character_,paste0("r",indices$r - 1,indices$i)),val = 10L,label = id) %>% 
        arrange(r,d,i)


# Sunburst
p <- plot_ly(df_indices,ids = ~id,labels = ~label,parents = ~parent,values = ~val,marker = list(colors = df_indices$col),type = 'sunburst',branchvalues = 'total'
)

p

图 1 是问题的一个示例 - “飞镖板”几乎是正确的。

几乎正确的“飞镖”森伯斯特图

enter image description here

图 2 显示了每个部分的位置。我通过将 Ring 4、Domain 1 中的指示器显示为灰色 (#757070) 来突出显示问题。

几乎正确的带有标签的“飞镖”森伯斯特图表

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)