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.
Step 1: Enable R Integration in SAC
Before using R scripts in SAC:
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
Step 3: Add and Execute the R Script
Click Add Script, and you will see four options: Editor, Console, Environment, and Preview.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 27 | |
| 24 | |
| 20 | |
| 20 | |
| 14 | |
| 13 | |
| 13 | |
| 12 | |
| 12 | |
| 11 |