Search code examples
colorsplotlyr-plotlysunburst-diagram

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:

This is the sunburst diagram I get using my code above, 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.


Solution

  • 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: enter image description here