‎2024 Nov 28 8:30 PM - edited ‎2024 Dec 09 3:46 AM
Hi,
In SAP Analytics Cloud, traditional charts are highly effective for most use cases. However, there are times when we need to push the boundaries and create something truly unique. This is where custom R visualizations come into play, offering a powerful way to go beyond standard capabilities.
Our goal was straightforward: we wanted to create a clean, interactive pie chart without the traditional lines pointing to the values, while also enhancing the overall presentation.
In this blog, we’ll walk you through the process of designing an interactive pie chart using R visualizations in SAP Analytics Cloud
Step 1: Creating a Widget
Click on Insert->Others -> R visualization
Step 2: Choosing the model
Click on ‘Add Input data’ on the right pane and choose the model which you are going to build and the corresponding measures and dimensions and click on Ok Button.
Step 3: Writing the R Script
Click on ‘add script’ and you will get the below window where you can write your R scripts
Step 4: Scripting using R
Write the scripts, click on 'Execute' button to preview and finally click apply
library(plotly)
library(dplyr)
# Create the data frame
df = data.frame(
product = MD_Test_Model$’Dimension 1’,
val1 = MD_Test_Mode$’Test Calculation’)
Note: After typing the model followed by $, when you press ‘Ctrl’ + Space, it lets you to choose the respective dimension/measure
# Summarize
df <- df %>%
group_by(product) %>%
summarise(val1 = sum(val1)) %>%
arrange(desc(val1)) %>%
mutate(percentage = val1 / sum(val1) * 100) # Calculate percentage for hover info
# Define custom colors for the pie chart
custom_colors <- c(
"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf")
# Creating the pie chart
plot_ly(df, labels = ~product, values = ~val1, type = 'pie',
textinfo = 'percent', # Shows percentages on the pie slices
hoverinfo = 'text', # Customizes hover information
text = ~paste("Value: ", val1, "<br>", "Percentage: ", round(percentage, 1), "%"), # Hover text
marker = list(colors = custom_colors) # Applies custom colors
) %>%
layout(title = "Title",
margin=list(l=10,r=30,t=30,b=10),
legend = list(
orientation = "h", # Places the legend horizontally
x = 0.5,y=1.45, xanchor = "center", # Centers the legend
font = list(size = 8 # Adjusts font size of the legend
)
)
The Pie Chart is Created
Request clarification before answering.
| User | Count |
|---|---|
| 9 | |
| 7 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.