# ggplot2 –> plotly, transferring subtitles and captions

Lets presume that a ggplot object(g) is available, and the idea is to convert this into a plotly object (p), which offers enhanced interactivity of plots.

However, the subtitle and captions defined in the said ggplot object do not get translated into plotly. This is a feature enhancement that has been raised in November 2016 on Github. Another reference issue in the plotly libraries on Github is also worth a quick look, because it seems to indicate that there is no subtitle function in the plotly library itself.

One workaround is described below. This basically defines the title, subtitle and captions as a plotly object (instead of ggplot). Though inconvenient, a tad inelegant (compared to ggplot syntax) and with syntax that is a little confusing – nevertheless, it is a solution.

## Note the structure of the arguments. THere are multiple 'titles' :)).  Note the HTML tags in < >. Only select tags are supported.

p  <- g %>%
ggplotly(tooltip = "all") %>%
layout(title = "This is the title <br> <sup> Subtitle using the superscript tag </sup>",
xaxis=list(title = 'Caption line 1 <br> Caption line 2'),
margin = list(l = 50, r = 50, t = 40, b = 70))

## Adjust the margin list to adjust the position of the caption. the arguments appears to transalate as the starting points of left, right, top, bottom.


## Other notes [0/1]

The query of the subtitles and captions disappearing when translated into plotly came to me attention as a question posted in the Business Science University forum.

It was fun to dig a little deeper and this is certainly worth keeping in mind while using plotly and ggplot.

Specific html tags are allowed in plotly. For example, the <sup> (superscript) tag had to be used, instead of <small>.

• [ ] I should think about constructing a snippet for ggplot objects, with all the arguments and formatting fed in. This would be useful to reduce fumbling around, looking for arguments and syntax.

## 2 responses on “ggplot2 –> plotly, transferring subtitles and captions”

1. shrysr says:

Plotly is basically an interactive web-browser based ‘charting’ library. i.e you can construct plots (either directly in plotly, or with some other library), and then use plotly to make the chart interactive allowing things like like zooming into a particular area, or getting tooltips with additional information when you hover over a point and etc. See https://bookdown.org/paulcbauer/idv2/7-1-what-is-plotly.html for more info.

ggplot is a library / system to create graphics or plots of data analyses, and is based on the grammar of graphics system. i.e layers, aesthetics and more. You can create a scatter plot on one layer, and add a line plot on top by adding another layer (with its own aesthetic settings). As such, ggplot’s syntax is quite intuitive and it is easier to construct plots in ggplot and then make them interactive via plotly (if necessary). Plotly’s interactive nature means it has to be viewed in a browser, and not a static PDF like document. See : https://ggplot2.tidyverse.org/