Lets presume that a
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.
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
[ ]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.