3 easy tricks for beginners to improve plotly charts in R | by Stefan Haring | Towards Data Science

We’ve all been there at the end of a data science project: we’ve cleaned all the data, we’ve explored it, gained valuable insight and made the results accessible to the business via a webtool. We have even gone so far and made awesome interactive visualisations using plotly!
The only problem is, the out-of-the-box formatting of those charts are mediocre. Co-workers are impressed with analytical insight but you hear them say things like “I wish the charts were nicer so we can show it to clients!” or “why is the cursor looking so weird?” and you’re left wondering how you can improve your charts for future projects.
If this is you (or if you generally want to customise your charts more), here are 3simple tricks to improve your plotly charts in R (these tricks are also easily portable to other languages like Python).
Before we start, we need a sample chart that we can improve upon. Below is a simple barchart displaying sales of three different products over the course of a year.
The modebar is the small menu on the top right hand side that shows up once you mouse-over the chart.
This feature can easily be disable, if you don’t want it to show up or if you just don’t want to provide certain features to users.
fig %>% config(displayModeBar = FALSE)
Alternatively, one can also customise the modebar and display only certain options, for example the download plot as image button.
fig %>% config(modeBarButtons = list(list("toImage")), displaylogo = FALSE, toImageButtonOptions = list(filename = "plotOutput.png"))
For more customisation possibilities, check out the plotly-r documentation here.
The standard cursor that shows up when hovering over a plotly chart is the crosshair. Some people may like that but personally I prefer the default arrow cursor.
To achieve this, the simplest way is to use the onRender function from the htmlwidgets package and a little bit of JavaScript.
fig %>% onRender("function(el, x) {Plotly.d3.select('.cursor-crosshair').style('cursor', 'default')}")
As we can see in our basic bar plot, the standard hover info that is displayed is (x value, y-value) and next to it the series name (in our case books, laptops or tvs). To add a little bit more information and formatting, we can create a separate variable and add it to the hover, e.g. if we wanted to show the percentage of total monthly sales we can create a new column with the percentage values and use that column for the text argument in our plot_ly function call.
#add percentages to our datadata <- data %>%  group_by(variable) %>%  mutate(pct = value / sum(value), pct = scales::percent(pct))#updated plot_ly function callplot_ly(x = ~dates, y = ~value, type = 'bar', text = ~pct, name = ~variable, color = ~variable)
If we want to take this a step further and show some custom text, there is no need to add another column to the data whose only use would be for displaying some text. We can just use the hovertemplate argument of plot_ly instead!
Various parts of our data set can be accessed by using the %{var} syntax, e.g. x and y values, the text argument and even the dataset itself (by using fullData.var, see the bonus section).
#updated plot_ly function callplot_ly(data, x = ~dates, y = ~value, type = 'bar', name = ~variable, color = ~variable, text = ~pct, hovertemplate = "Units sold: %{y} <br> Percentage: %{text}")
While this hoverinfo already looks much better, wouldn’t it be nicer if “Units sold” would be replaced with e.g. tvs sold instead of displaying the series name outside of the box? Easy! Access the series names with ${fullData.name} and add it to the hovertemplate. To remove the extra text next to the box, add the empty html tag <extra> and don’t forget to close it!
#updated plot_ly function callplot_ly(data, x = ~dates, y = ~value, type = 'bar', name = ~variable, color = ~variable, text = ~pct, hovertemplate = "%{fullData.name} sold: %{y}<br>Percentage: %{text} <extra></extra>")
In this post, I’ve demonstrated 3 quick and easy tricks that you can immediately use when starting out with plotly in R. These methods allow you to enhance and customise certain aspects of your charts and enable you and your users to gain more insight from the data. And all that in just a few simple lines of code and without any complicated formatting!