cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Pie Chart using R visualization in SAP Analytics Cloud

tharunbalaji
Discoverer
1,096

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

tharunbalaji_0-1732734245620.png

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.

tharunbalaji_1-1732734320085.png

Step 3: Writing the R Script

Click on ‘add script’ and you will get the below window where you can write your R scripts

tharunbalaji_2-1732734351207.png

tharunbalaji_3-1732734358014.png

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

    )

  )

tharunbalaji_0-1732825105652.png

The Pie Chart is Created 

 

 

 

Accepted Solutions (0)

Answers (0)