Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly adding a source or caption to a chart

Tags:

r

plotly

Is there a way to add a data source / caption to a chart in Plotly, similar with what can be done in ggplot with the caption argument:

labs(caption = "source: data i found somewhere") 

i.e., so we can display the data source at the bottom right of the graph, in a smaller font.

like image 361
chrisjacques Avatar asked Jul 14 '17 12:07

chrisjacques


People also ask

How do you add captions on Plotly?

In plotly, we can achieve a subtitle by adding some HTML formatting to the title string. We will Bold the title, emphasize it, add a line break, then use a span element with a reduced font size to add a subtitle. And that's it!

How do you add text to a Plotly graph?

Standalone text annotations can be added to figures using fig. add_annotation() , with or without arrows, and they can be positioned absolutely within the figure, or they can be positioned relative to the axes of 2d or 3d cartesian subplots i.e. in data coordinates.


1 Answers

annotation offers a simple way to add a caption to a chart in plotly:

library(plotly)
plot_ly(x=~hp, y=~mpg, data=mtcars, type="scatter", mode="marker") %>% 
 layout(annotations = 
 list(x = 1, y = -0.1, text = "Source: data I found somewhere.", 
      showarrow = F, xref='paper', yref='paper', 
      xanchor='right', yanchor='auto', xshift=0, yshift=0,
      font=list(size=15, color="red"))
 )

enter image description here .

More details are given here and here.

like image 146
Marco Sandri Avatar answered Sep 21 '22 00:09

Marco Sandri