无法在绘图旭日图中显示正确的颜色

问题描述 投票:0回答:1

我正在尝试在 R 中使用plotly 制作旭日图。但是,我无法正确显示颜色。

这是我的代码:

library(plotly)
test <- data.frame(
  stringsAsFactors = FALSE,
  ids = c("pH", "SpecificConductance","TotalNitrogen","TotalPhosphorous","OE_Macroinvertebrate",
          "PctBankOverheadCover","PctFinesLessThan2mm","PctBankCovered","PctBankStable",
          "FloodplainConnectivity","WaterQuality","Biodiversity","WatershedFunction",
          "SiteScore"),
          
  labels = c("pH<br>A",
             "SpecificConductance <br> A", "TotalNitrogen <br> A","TotalPhosphorous <br> F",
             "OE_Macroinvertebrate <br> B", "PctBankOverheadCover <br> B",
             "PctFinesLessThan2mm <br> A","PctBankCoveredMIM <br> B",
             "PctBankStable <br> A","FloodplainConnectivity <br> F",
             "WaterQuality <br> B","Biodiversity <br> B",
             "WatershedFunction <br> B", "SiteScore <br> B"),
  parents = c("WaterQuality", "WaterQuality","WaterQuality",
              "WaterQuality","Biodiversity","Biodiversity","WatershedFunction","WatershedFunction",
              "WatershedFunction","WatershedFunction","SiteScore","SiteScore",
              "SiteScore",
              NA ),
  colors = c("#481567FF","#481567FF","#481567FF","#FDE725FF","#238A8DFF","#238A8DFF",
    "#481567FF","#238A8DFF","#481567FF","#FDE725FF","#238A8DFF","#238A8DFF","#238A8DFF",
    "#238A8DFF"
  )
)

test_plot <- plot_ly(test, ids = ~ids, labels = ~labels, parents = ~parents, 
                       type = 'sunburst'
) %>% layout(colorway = ~colors)

test_plot

这是我得到的输出。然而,这些不是我的代码指定的颜色:应该是紫色、青色和黄色的混合,并且中心也应该是彩色的。知道如何修复彩色显示吗?我正在使用 R 版本 4.4.0

sunburst

snippet of code with colors

我已经尝试过处理堆栈溢出的示例(Plotly sunburst coloring)并且可以复制这些示例,但是当我用我的数据替换时它无法正常工作

r plotly sunburst-diagram
1个回答
0
投票

为了达到您想要的结果,请通过迹线的

marker=
属性设置颜色。通过这种方式,您可以为旭日的每个部分设置颜色。使用
colorway=
您只需设置默认颜色,plotly 将根据父类别的数量从中选择颜色,即它将从您的颜色向量中选择前 3 种颜色。

library(plotly)

plot_ly(test,
  ids = ~ids, labels = ~labels, parents = ~parents,
  marker = list(
    colors = ~colors
  ),
  type = "sunburst"
)

© www.soinside.com 2019 - 2024. All rights reserved.