SAP Analytics Cloud (SAC) offers a robust suite of visualization tools but sometimes, more advanced or specialized visualizations like the Marimekko chart require custom scripting.
In this post, I’ll walk you through how I extended SAC’s capabilities using the R visualization widget to create an interactive Marimekko chart. (specific to EHS module).
What is a Marimekko Chart?
A Marimekko chart is a bar-style chart used to communicate two measures by varying the width and height of bars, while also representing two dimensions by stacking values within each bar and 100% bar chart.
Mandatory Attributes: Requires at least 1 Measure (Height) and 1 Dimension.
Limitation to use Built-In chart – Doesn’t support negative values as shown below
Use Case Scenario – EHS Module
Let’s say we want to visualize “Marginal Abatement Cost Curve (MACC)” which is related to EHS.
MACC Curve indicates how profitable it can be to invest in more sustainable technologies.
A Marimekko chart allows us to represent both MACC value (Measure1) and Average carbon reduction (Measure2) by Projects (Dimension), in one compact visual. (The lower the MACC value the better it is!)
Step-by-Step Creating the Marimekko Chart in using R widget in SAC :
Insert R Visualization widget in the Story and connect it to the desired modelAdd the relevant measures and dimensions in the builder Input data panelR script for the Marimekko Chart –
# Load required libraries
library(ggplot2) # For creating plots
library(dplyr) # For data manipulation
library(plotly) # For making plots interactive
# Create a data frame using specific required dimensions and measures from an existing dataset
data <- data.frame(
group = <Model name>$<Dimension>, # Dimension is Project in this scenario
MACC_value = <Model name>$<Measure1>, # Consider Measure to represent height
Averagecarbon_reduction = round(<Model name>$<Measure2>) # Consider Measure to represent width
)
# Sort data in ascending order of MACC value for logical ordering in the chart
data <- data %>% arrange(MACC_value)
# Calculate the future positions on the x-axis (width of each bar) and the y-values
data$right <- cumsum(data$Averagecarbon_reduction) + 20 * c(0:(nrow(data) – 1)) #right end of each bar
data$left <- data$right – data$Averagecarbon_reduction #left end of each bar
data$center <- (data$left + data$right) / 2 #center position for tooltips
# Assign a color group based on the MACC value
data <- data %>% mutate(
color_group = case_when(
MACC_value <= 0 ~ “green”, # Green for cost-saving projects
MACC_value > 0 & MACC_value <= 40 ~ “orange”, # Orange for moderate-cost projects
MACC_value > 40 ~ “red” # Red for expensive projects
)
)
# Create a Marimekko chart using ggplot2
p <- ggplot(data, aes(
xmin = left, xmax = right, ymin = 0, ymax = MACC_value, fill = color_group,
text = paste(
“Average Carbon Reduction (100T carbon/year): “, Averagecarbon_reduction,
“<br>MACC ($US/T CO2 e): “, MACC_value,
“<br>Project Name: “, group
)
)) +
# Draw rectangles for each project
geom_rect() +
# Custom fill colors
scale_fill_manual(values = c(“green” = “#20a38b”, “orange” = “#eab615”, “red” = “#f16665”)) +
# Axis labels
labs(x = “Average Carbon Reduction (100T carbon/year)”, y = “MACC Value”) +
# Use a clean theme
theme_minimal() +
# Remove legend since colors are self-explanatory
theme(legend.position = “none”)
# Convert the static ggplot to an interactive Plotly plot
p_interactive <- ggplotly(p, tooltip = “text”) # Enable tooltips with specified text
# Optional customization: Set tooltip text color to white
p_interactive <- p_interactive %>% layout(
hoverlabel = list(
font = list(color = “white”)
)
)
# Display the interactive chart
p_interactive
Below is the R visualization of above code along with tooltip
Conclusion:
Now we have created Marimekko chart using R Visualizations in SAP Analytics Cloud.
Thank you for taking the time to read my blog and I hope you find it beneficial!
SAP Analytics Cloud (SAC) offers a robust suite of visualization tools but sometimes, more advanced or specialized visualizations like the Marimekko chart require custom scripting.In this post, I’ll walk you through how I extended SAC’s capabilities using the R visualization widget to create an interactive Marimekko chart. (specific to EHS module).What is a Marimekko Chart?A Marimekko chart is a bar-style chart used to communicate two measures by varying the width and height of bars, while also representing two dimensions by stacking values within each bar and 100% bar chart.Mandatory Attributes: Requires at least 1 Measure (Height) and 1 Dimension.Limitation to use Built-In chart – Doesn’t support negative values as shown belowUse Case Scenario – EHS ModuleLet’s say we want to visualize “Marginal Abatement Cost Curve (MACC)” which is related to EHS.MACC Curve indicates how profitable it can be to invest in more sustainable technologies.A Marimekko chart allows us to represent both MACC value (Measure1) and Average carbon reduction (Measure2) by Projects (Dimension), in one compact visual. (The lower the MACC value the better it is!)Step-by-Step Creating the Marimekko Chart in using R widget in SAC :Insert R Visualization widget in the Story and connect it to the desired modelAdd the relevant measures and dimensions in the builder Input data panelR script for the Marimekko Chart -# Load required librarieslibrary(ggplot2) # For creating plotslibrary(dplyr) # For data manipulationlibrary(plotly) # For making plots interactive# Create a data frame using specific required dimensions and measures from an existing datasetdata <- data.frame( group = <Model name>$<Dimension>, # Dimension is Project in this scenario MACC_value = <Model name>$<Measure1>, # Consider Measure to represent height Averagecarbon_reduction = round(<Model name>$<Measure2>) # Consider Measure to represent width)# Sort data in ascending order of MACC value for logical ordering in the chartdata <- data %>% arrange(MACC_value)# Calculate the future positions on the x-axis (width of each bar) and the y-valuesdata$right <- cumsum(data$Averagecarbon_reduction) + 20 * c(0:(nrow(data) – 1)) #right end of each bardata$left <- data$right – data$Averagecarbon_reduction #left end of each bardata$center <- (data$left + data$right) / 2 #center position for tooltips# Assign a color group based on the MACC valuedata <- data %>% mutate( color_group = case_when( MACC_value <= 0 ~ “green”, # Green for cost-saving projects MACC_value > 0 & MACC_value <= 40 ~ “orange”, # Orange for moderate-cost projects MACC_value > 40 ~ “red” # Red for expensive projects ))# Create a Marimekko chart using ggplot2p <- ggplot(data, aes( xmin = left, xmax = right, ymin = 0, ymax = MACC_value, fill = color_group, text = paste( “Average Carbon Reduction (100T carbon/year): “, Averagecarbon_reduction, “<br>MACC ($US/T CO2 e): “, MACC_value, “<br>Project Name: “, group ))) +# Draw rectangles for each project geom_rect() + # Custom fill colors scale_fill_manual(values = c(“green” = “#20a38b”, “orange” = “#eab615”, “red” = “#f16665”)) + # Axis labels labs(x = “Average Carbon Reduction (100T carbon/year)”, y = “MACC Value”) + # Use a clean theme theme_minimal() + # Remove legend since colors are self-explanatory theme(legend.position = “none”) # Convert the static ggplot to an interactive Plotly plotp_interactive <- ggplotly(p, tooltip = “text”) # Enable tooltips with specified text# Optional customization: Set tooltip text color to whitep_interactive <- p_interactive %>% layout( hoverlabel = list( font = list(color = “white”) ))# Display the interactive chartp_interactiveBelow is the R visualization of above code along with tooltipConclusion:Now we have created Marimekko chart using R Visualizations in SAP Analytics Cloud.Thank you for taking the time to read my blog and I hope you find it beneficial! Read More Technology Blogs by Members articles
#SAP
#SAPTechnologyblog