Working with R Visualization Components

Objectives

After completing this lesson, you will be able to:

  • Explain R visualization components

Access R in an SAP Analytics Cloud Story

Add R Visualizations from the Story Menu

Inserting an R visualization in your story is easy! You can add the R visualization from the toolbar Add menu option or you can drag the widget from the Assets panel and drop it into the story.

An SAP Analytics Cloud story with the Asset panel open on the left hand side of the screen and R Visualization widget highlighted. Also, the Add menu drop down is open with R Visualization highlighted.

If the Add menu option isn't visible in your toolbar, then you use the More menu option to add the R visualization widget to your story.

An SAP Analytics Cloud story with the More menu item and menu path to R Visualization widget highlighted.

R Visualization Components

An R visualization is based on the following components:

  1. Table structure: Input data based on a model, specified dimensions, dimension attributes, and filters
  2. Input controls: Input parameters based on a calculation input control
  3. R code script: A script executed to generate the visualization

Table Structure

Once you've created your story, selected your data source, and added your R visualization to the story, you can change the table structure of the R visualization in the Builder panel on the right side of the screen.

Builder panel with Add Input Data highlighted.
  1. Select + Add Input Data as highlighted in the preceding image to open the Input Data panel. You can select the Expand icon in the Input Data panel preview the input data table structure. Input Data panel with Expand highlighted.
  2. Use the Table Structure settings to specify the table structure for your input data.
    1. Rows: Choose +Add Dimensions to select dimensions from the list of available options to add them to your input data. For example, if you wanted to display product groups in your table.
    2. Columns: Use this to manage the available measures. For example, to add a country to your table. Choose All Members to display the currently available measures.
    3. Filters: Use this to manage the filters for the input data. For example, to filter data to display a specific year.
The Rows (a), Columns (b), and Filters (c) displayed in the table structure of the R visualization.

Input Controls

You can also configure an input parameter for your visualization.

When you select Add Input Parameter, you're able to select an input control. If the story already contains calculation input controls, you're able to choose any of the listed parameters.

In the following example, you can see two options listed under the Name field. You can choose between:

  1. Existing Dimension to allow users to pick from members of a dimension
  2. Static List to add custom values as options for the input control

In the following example, Existing Dimension was selected.

Dialog for calculation input control, showing Name, Properties (Model and Dimension) and Input Values. Name is a mandatory field and is highlighted in red in the system until a value is entered.

R Code Script

In this example, you can see the data, R code, and the final visualization created.

Later in this course, you will create the R visualization used in this example.

The Data

Input Data panel with data preview expanded.

The R Code

Code snippet
library(ggplot2)
library(dplyr)
library(grid)
library(gridExtra)

g1 <- ggplot(BestRunCorpRetail, aes(x = reorder(Lines,`Sales Revenue`/1000), y = `Sales Revenue`/1000 , fill = `Region` )) +
  geom_col(stat = "identity" ) + coord_flip() +
  scale_fill_brewer(palette="Blues") +
  labs(title = " Sales Revenue (in k) by Product Line / Region", x = "Product Lines / Region ", y = " Sales Revenue") + 
  theme_light() + 
  theme(legend.position="bottom")

g2 <- ggplot(BestRunCorpRetail, aes(x = reorder(Lines,`Gross Margin`/1000), y = `Gross Margin`/1000 , fill = `Region` )) +
  geom_col(stat = "identity" ) + coord_flip() +
  scale_fill_brewer(palette="Reds") +
  labs(title = " Gross Margin (in k) by Product Line / Region", x = "Product Lines / Region ", y = "Gross Margin") + 
  theme_light() + 
  theme(legend.position="bottom")

gg1 <- ggplot_gtable(ggplot_build(g1))
gg2 <- ggplot_gtable(ggplot_build(g2))

grid.arrange(gg1, gg2, ncol = 2, widths = c(1/2,1/2))
Expand

The Final R Visualization

When your R code has been written, you can add the controls to the R visualization in SAP Analytics Cloud. The R code and the controls enable you to create the visualization, in this example, comparing Sale Revenue and Gross Margin by Product Line/Region with an input control for Year.

Final R Visualization created using the code above the image.

Log in to track your progress & complete quizzes