Search code examples
rcolorssunburst-diagram

How to manually colour sunburst() chart so that leafs are different shade of same colour to parent leaf?


I want to manually colour a sunburst chart where layers that branch from the inner circle is the same colour but a different shade as you move out from the center. In my particular dataframe I therefore want all parts of the circles (from the 2'nd circle to the last circle) that branch out from defined Taxa groups (i.e., the first circle - parent leaf) to be different shades of the same colour. For example, where I have Cetacea in my first circle I will colour orange, and every circle portion branching from this group will be a different shade of orange moving from darker to lighter as you move towards the outer circle. Is this possible? Any idea how to do this?

Here is a sample of my dataframe

df = structure(list(V1 = c("Cephalopoda - Saltwater - Behavioural Ecology - Behavioural states and adaptations - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                           "Cephalopoda - Saltwater - Behavioural Ecology - Habitat and space use - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                           "Cephalopoda - Saltwater - Conservation Methods - Hypoxic Zones - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                           "Cephalopoda - Saltwater - Other Drivers of Movement and Habitat Use - Environmental factors - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                           "Cephalopoda - Saltwater - Other Drivers of Movement and Habitat Use - Seasonal Differences - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Behavioural states and adaptations - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Co_existence - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Co_existence - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Cooperative Behaviour - 3m to 40m - Medium - Medium Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Energetics - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Energetics - >40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Predator prey interactions - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Behavioural Ecology - Predator prey interactions - >40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Methodological - Feasibility - Surface to 3m - Medium - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Methodological - Feasibility - Surface to 3m - NA - Short Term - Short term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Methodological - Feasibility - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Methodological - Tagging procedure - Surface to 3m - Medium - NA - NA - Satellite - Stationary", 
                           "Cetacea - Saltwater - Other Drivers of Movement and Habitat Use - Environmental factors - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Other Drivers of Movement and Habitat Use - Ice cover and shift - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Reproductive Ecology - Parental offspring interactions - Surface to 3m - Medium - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Cetacea - Saltwater - Reproductive Ecology - Parental offspring interactions - Surface to 3m - NA - Short Term - Short term - Archival - AnimalBorne", 
                           "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - Medium - Long Term - Long Term - Acoustic_Satellite - BRUV", 
                           "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - Large - Medium Term - Medium Term - Archival - AnimalBorne", 
                           "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - >40m - Large - Short Term - Medium Term - Archival - AnimalBorne", 
                           "Chondrichthyes - Saltwater - Behavioural Ecology - Co_existence - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                           "Chondrichthyes - Saltwater - Behavioural Ecology - Community structuring - Surface to 3m - Small - Long Term - Short term - Acoustic - BRUV"
), total = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
             1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 
                                                                                         -27L), class = c("tbl_df", "tbl", "data.frame"))

and here the code I am using

library(sunburstR)
p = sunburst(df)
p

An example of what I am looking for is something like this. Please note that it is the progression of colour, and colour groupings that are important here. The lines are not exactly where they fall when you run my code enter image description here


Solution

  • If I understand correctly, I think supplying a JavaScript function will likely be the best method to achieve your goal. Will you please try the following and let me know if this is in the right direction?

    library(sunburstR)
    
    df = structure(list(V1 = c("Cephalopoda - Saltwater - Behavioural Ecology - Behavioural states and adaptations - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                               "Cephalopoda - Saltwater - Behavioural Ecology - Habitat and space use - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                               "Cephalopoda - Saltwater - Conservation Methods - Hypoxic Zones - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                               "Cephalopoda - Saltwater - Other Drivers of Movement and Habitat Use - Environmental factors - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                               "Cephalopoda - Saltwater - Other Drivers of Movement and Habitat Use - Seasonal Differences - NA - Medium - Medium Term - NA - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Behavioural states and adaptations - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Co_existence - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Co_existence - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Cooperative Behaviour - 3m to 40m - Medium - Medium Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Energetics - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Energetics - >40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Predator prey interactions - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Behavioural Ecology - Predator prey interactions - >40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Methodological - Feasibility - Surface to 3m - Medium - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Methodological - Feasibility - Surface to 3m - NA - Short Term - Short term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Methodological - Feasibility - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Methodological - Tagging procedure - Surface to 3m - Medium - NA - NA - Satellite - Stationary", 
                               "Cetacea - Saltwater - Other Drivers of Movement and Habitat Use - Environmental factors - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Other Drivers of Movement and Habitat Use - Ice cover and shift - 3m to 40m - NA - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Reproductive Ecology - Parental offspring interactions - Surface to 3m - Medium - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Cetacea - Saltwater - Reproductive Ecology - Parental offspring interactions - Surface to 3m - NA - Short Term - Short term - Archival - AnimalBorne", 
                               "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - Medium - Long Term - Long Term - Acoustic_Satellite - BRUV", 
                               "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - 3m to 40m - Large - Medium Term - Medium Term - Archival - AnimalBorne", 
                               "Chondrichthyes - Saltwater - Behavioural Ecology - Behavioural states and adaptations - >40m - Large - Short Term - Medium Term - Archival - AnimalBorne", 
                               "Chondrichthyes - Saltwater - Behavioural Ecology - Co_existence - >40m - NA - Medium Term - Medium Term - Archival - AnimalBorne", 
                               "Chondrichthyes - Saltwater - Behavioural Ecology - Community structuring - Surface to 3m - Small - Long Term - Short term - Acoustic - BRUV"
    ), total = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 
                                                                                             -27L), class = c("tbl_df", "tbl", "data.frame"))
    sunburst(
      df,
      withD3 = TRUE,
      colors= htmlwidgets::JS("
    function() {
      debugger
      // this will be d3 version 4
      const node = d3.select(this).datum()
      let color
      
      if(node.depth > 0) {  // 2nd level
        const ancestors = node.depth === 1 ? [node.parent, node] : node.ancestors().reverse()
        // inefficient to define every time but will do this way for now
        color = d3.scaleOrdinal(d3.schemeCategory10)
          .domain(ancestors[0].children.map(d=>d.data.name))
        return(d3.color(color(ancestors[1].data.name)).brighter((node.depth - 1)/4))
      }
    }
      ")
    )
    
    

    screenshot of sunburst