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
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.
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.
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.
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:
Use the function reorder() to sort bar based on a measure.
Tooltip can be configured by assigning any measure to hoverinfo option.
c. Bar Color
The color of the bar can be easily changed within marker option.
2. Enhancing the plot using layout()
Margins can be customized within layout() function using margin option.
b. Auto Size
autosize = Twill auto size the chart.
c. Axis Line
Linewidth option is used to assign line width to axis.
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.
e. Format Tooltip
The tooltip added previously can be formatted here using hoverlabel.
Properties like color, family can also be changed.
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.
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.
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.