简体   繁体   中英

How to map discrete colours in a Plotly Sunburst chart in r

I am very new to using plotly in rstudio and have come up against a problem with mapping discrete colours (stored as hex codes in the field color ) to each of the slices in my ids field.

I have included my code below:

df %>%
  plot_ly(
     color = I("black"),
     marker = list(colors = ~color)) %>% 
  add_trace(ids = df$ids, 
            labels = df$labels, 
            parents = df$parents,
            type = 'sunburst',
            maxdepth = -1,
            domain = list(column = 0)) %>%
  layout(sunburstcolorway = df$color)

This is the resulting sunburst diagram I get using this code, which is obviously not ideal:

这是我使用上面的代码得到的旭日形图,显然不理想

Ideally the first four levels would have the same colour, and then different hex colour codes are used for slices that are labelled "Poor","Moderate","GwC" or "Good".

A csv file of my data frame used above is available here .

I finally managed to nut out how to map my colour field to the background colours on the sunburst chart - have updated the code in original post. All that was required was to insert the following code segment:

plot_ly(
    marker = list(colors = ~color))

Below is the output chart: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM