How to make a sunburst plot in R or Python?

前端 未结 8 1622
梦如初夏
梦如初夏 2020-11-28 22:54

So far I have been unable to find an R library that can create a sunburst plot like those by John Stasko. Anyone knows how to accomplish that in R or Python?

相关标签:
8条回答
  • 2020-11-28 23:37

    Here is an example using R and plotly (based on my answer here):

    library(datasets)
    library(data.table)
    library(plotly)
    
    as.sunburstDF <- function(DF, valueCol = NULL){
      require(data.table)
      
      colNamesDF <- names(DF)
      
      if(is.data.table(DF)){
        DT <- copy(DF)
      } else {
        DT <- data.table(DF, stringsAsFactors = FALSE)
      }
      
      DT[, root := names(DF)[1]]
      colNamesDT <- names(DT)
      
      if(is.null(valueCol)){
        setcolorder(DT, c("root", colNamesDF))
      } else {
        setnames(DT, valueCol, "values", skip_absent=TRUE)
        setcolorder(DT, c("root", setdiff(colNamesDF, valueCol), "values"))
      }
      
      hierarchyCols <- setdiff(colNamesDT, "values")
      hierarchyList <- list()
      
      for(i in seq_along(hierarchyCols)){
        currentCols <- colNamesDT[1:i]
        if(is.null(valueCol)){
          currentDT <- unique(DT[, ..currentCols][, values := .N, by = currentCols], by = currentCols)
        } else {
          currentDT <- DT[, lapply(.SD, sum, na.rm = TRUE), by=currentCols, .SDcols = "values"]
        }
        setnames(currentDT, length(currentCols), "labels")
        hierarchyList[[i]] <- currentDT
      }
      
      hierarchyDT <- rbindlist(hierarchyList, use.names = TRUE, fill = TRUE)
      
      parentCols <- setdiff(names(hierarchyDT), c("labels", "values", valueCol))
      hierarchyDT[, parents := apply(.SD, 1, function(x){fifelse(all(is.na(x)), yes = NA_character_, no = paste(x[!is.na(x)], sep = ":", collapse = " - "))}), .SDcols = parentCols]
      hierarchyDT[, ids := apply(.SD, 1, function(x){paste(x[!is.na(x)], collapse = " - ")}), .SDcols = c("parents", "labels")]
      hierarchyDT[, c(parentCols) := NULL]
      return(hierarchyDT)
    }
    
    DF <- as.data.table(Titanic)
    setcolorder(DF, c("Survived", "Class", "Sex", "Age", "N"))
    sunburstDF <- as.sunburstDF(DF, valueCol = "N")
    
    # Sunburst
    plot_ly(data = sunburstDF, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')
    
    # Treemap
    # plot_ly(data = sunburstDF, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='treemap', branchvalues = 'total')
    

    Some additional information can be found here.

    0 讨论(0)
  • 2020-11-28 23:38

    Theres a package called ggsunburst. Sadly is not in CRAN but you can install following the instruction in the website: http://genome.crg.es/~didac/ggsunburst/ggsunburst.html.

    Hope it helps to people who still looking for a good package like this.

    Regards,

    0 讨论(0)
提交回复
热议问题