This blog series is about leveraging R Visualizations in SAP Analytics Cloud. In the previous blog, we learnt how to create R widgets in an analytic application and perform dynamic filtering on them. In this blog, let us see how to add interactive tooltips, data labels and other customizations to enhance the user experience in R charts of SAP Analytics Cloud.

 

The plotly library

While most R packages render visuals as PNG images, plotly being a javascript graphing library allows the user to create interactive charts. Here you can see the fully customized R Visualization using the plotly library.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

Now, let us see how to add a simple plotly chart and customize it for SAP Analytics Cloud Stories / Applications to suit our requirements. Though the customizations discussed in this blog can be used for all chart types, for easier understanding, we’ll consider using a bar chart.

 

Adding interactivity to R widgets using the plotly library

plot_ly() function is used to plot various visualizations. The following script renders a simple bar chart. Refer to the previous blog to learn about configuring R widgets in SAP Analytics Cloud.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

Executing the script renders an unsorted bar chart along with tooltip. By default, the tooltip is configured to show the measure value and the name of the member.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

Also, there are various options in the mode barincluding zoom, export, pan, auto scale etc. It even allows the user to compare two data points on hover, like native charts.

 

Customizing R Widgets

Although the chart plotted using plot_lyprovides various features to the end user like mode bar, note that the chart currently looks drastically different from other native charts in any Story/App and also that the y-axis labels are cut by default as the label length is too large to fit.

One need not go through the hassle of these aesthetic discrepancies to leverage the capabilities of R in SAP Analytics Cloud. There are ways to extensively customize the looks of the chart as explained below.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

Plotly provides multiple functions and numerous tweak options that make the visualizations highly customizable. Along with the default plot_ly() function, let’s make use of layout() and config() functionalities to make it aesthetically appealing as the native ones.

 

1. Leveragingplot_ly() for interactivity

Following are the options that can be customized under plot_ly() function:

a. Sort

Use the function reorder() to sort bar based on a measure.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

b. Tooltip

Tooltip can be configured by assigning any measure to hoverinfo option.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

c. Bar Color

The color of the bar can be easily changed within marker option.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

2. Enhancing the plot using layout()

a. Margin

Margins can be customized within layout() function using margin option.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

b. Auto Size

autosize = Twill auto size the chart.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

c. Axis Line

Linewidth option is used to assign line width to axis.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

d. Axis Labels and Gridlines

You can remove axis labels by setting showticket labels to false and hide the gridlines by setting the option showgrid to false.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

e. Format Tooltip

The tooltip added previously can be formatted here using hoverlabel.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

f. Font

Properties like color, family can also be changed.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

3. Customizing Data Labels

The function add_annotations() can be used to show any measure and can also be formatted using options like xanchor to align the data labels.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

 

4. Customizing Mode Bar

displayModeBar option is used to set the mode bar option using the config() function.

 

The code above customizes R widgets in SAP Analytics Cloud, to resemble native charts. Apart from the options mentioned above, you can find plenty more for widget customizations here.

These customizations can also be applied to different chart types as shown below.

r-visualizations-sap-analytics-cloud-series-2-adding-interactivity-customizations-r-widgets

 

In the next blog of this series, we will look at how to dynamically swap, sort and rank visualizations in SAP Analytics Cloud using R.

Reach out to our team here if you are interested in evaluating if SAP Analytics Cloud is right for you.

Subscribe to our Newsletter

5920 Windhaven Pkwy
Plano TX 75093.

+1 888-227-2794

+1 972-232-2233

+1 888-227-7192

solutions@visualbi.com