使用 Rplot_ly 更改旭日图的颜色

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

我使用plot_ly将我的数据可视化为旭日形,就像之前使用Adobe Illustrator所做的那样,但未能同样为片段着色。

这是代码(颜色代码是随机的):

library(plotly)
tab <- data.frame(
  labels = c("Total", "Planerlöse", "Abgeltung", "Bund", "Kanton Thurgau", "Kanton Thurgau Netto", "Gemeinden" ),
  parents = c("", "Total", "Total", "Abgeltung", "Abgeltung", "Kanton Thurgau", "Kanton Thurgau"),
  values = c(100, 46,54, 23.76, 30.24, 20.26, 9.97),
  colors=c("#f28f1c", "#fef4e8", "#2771b0", "#e9f1f7",
           "#b5b5b5", "#787878", "#333333"),
  type = 'sunburst',
  branchvalues = 'total'
)

fig <- plot_ly(tab, labels = ~labels, parents = ~parents, 
                      values = ~values, colors=colors, type = 'sunburst', branchvalues = 'total'
)

fig

这里是我想复制的原始情节:

感谢您的建议和帮助!

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

您可以使用

layout
 添加 
colorway
将颜色添加到旭日图中,如下所示:

library(plotly)
library(dplyr)

fig <- plot_ly(tab, labels = ~labels, parents = ~parents, 
               values = ~values, type = 'sunburst', branchvalues = 'total'
) %>% layout(colorway = ~colors)

fig

创建于 2023-08-07,使用 reprex v2.0.2

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