Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
pratyushg13
Explorer
1,487

A Sunburst chart is a powerful visualization technique for representing hierarchical data. It starts from a central root node and radiates outward to represent the hierarchy across multiple levels. In SAP Analytics Cloud (SAC), you can create this type of chart using R scripting and Plotly, providing a dynamic and interactive experience.

This guide walks you through the steps to build a Sunburst chart inside an SAC story using R.

What Is a Sunburst Chart?

A Sunburst chart visualizes multilevel categorical data in a circular layout. Each level of the hierarchy is represented by one ring or circle, with the innermost circle being the root node. Plotly in R provides excellent support for building dynamic Sunburst visualizations.pratyushg13_0-1748947238564.png

Step 1: Enable R Integration in SAC

Before using R scripts in SAC:

  1. Open the Main Menu.
  2. Navigate to System > Administration > External Systems.
  3. Ensure the R connection is enabled.

pratyushg13_1-1748947301491.png

If not, refer to the official SAP guide for integration:
https://help.sap.com/docs/SAP_ANALYTICS_CLOUD/00f68c2e08b941f081002fd3691d86a7/8d589100aea341a58a5c0...

Step 2: Insert an R Visualization in Your Story

  1. Create or open a story.
  2. Go to the Insert section, click the +, then choose R Visualization.
  3. A tile will be added to your story.
  4. Click the tile and go to the Builder Panel on the right.
  5. Click + Add Input Data and select the required dimensions and measures (e.g., Version, Plants, Product, M1).

pratyushg13_2-1748947344993.png

Step 3: Add and Execute the R Script

Click Add Script, and you will see four options: Editor, Console, Environment, and Preview.

pratyushg13_3-1748947366540.png

Paste the following R script into the Editor section. (Note: Adjust dimension/measure names as per your model)

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)
}

# Example data setup
data11 = Test_Model
data22 = subset(data11, select = c("Version", "Plants", "Product", "M1"))
colnames(data22) <- c("Version", "Plants", "Product", "M1")

DF <- as.data.table(data22)
setcolorder(DF, c("Version", "Plants", "Product", "M1"))
sunburstDF <- as.sunburstDF(DF, valueCol = "M1")

plot_ly(
  data = sunburstDF,
  ids = ~ids,
  labels = ~labels,
  parents = ~parents,
  values = ~values,
  type = 'sunburst',
  branchvalues = 'total',
  marker = list(
    colors = c('white', "e6ccb2", "79b2d2", "a1c65d", "fac723", "f29222", "e95e50", "ff92b1")
  )
)

Click Execute, and the preview pane will display the generated Sunburst chart. Hit Apply to render it on the story canvas.

Conclusion

Using R and Plotly inside SAP Analytics Cloud, you can build highly interactive and insightful Sunburst charts to visualize hierarchical datasets. This approach not only expands SAC's native visualization capabilities but also gives you flexibility to tailor visuals to your data story.

Explore more possibilities with R integration in SAC and turn your dashboards into truly dynamic visual experiences.

1 Comment