19

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.

lmo
  • 37,904
  • 9
  • 56
  • 69
chrisjacques
  • 635
  • 1
  • 5
  • 17

3 Answers3

37

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.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • Thanks Marco, but ggplot already has labs for that. My question is specifically to do that but in plotly. – chrisjacques Jul 25 '17 at 08:36
  • 1
    @chrisjacques I edited my answer. Now you can find a way to add a caption to your plotly chart. If this solution can help you, please consider to upvote it. – Marco Sandri Jul 25 '17 at 09:43
2

If you want to use a ggplot graph, you can convert it to plotly using ggplotly and still add a caption using the same layout function like this:

library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() 

ggplotly(p) %>%
  layout(margin = list(l = 50, r = 50, b = 100, t = 50),
         annotations = list(x = 1, y = -0.3, text = "Source: data I found somewhere.",
                            xref='paper', yref='paper', showarrow = F, 
                            xanchor='right', yanchor='auto', xshift=0, yshift=0,
                            font = list(size = 10)))

Output:

enter image description here

So this makes it possible to use ggplot as a plotly graph.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Can `Source` be made bold by adding `**Source**`? – Ed_Gravy Oct 10 '22 at 13:53
  • 1
    Hi @Ed_Gravy, If you want to make `Source` bold, you could use HTML syntax like this: `Source` which will make it bold. – Quinten Oct 10 '22 at 13:56
  • Thank you, should I post this as a question so others might find it useful too? I will using your code as reference though? – Ed_Gravy Oct 10 '22 at 13:57
  • 1
    @Ed_Gravy, No problem! Glad it helped. That could be useful indeed. Maybe this [answer](https://stackoverflow.com/questions/50067301/make-plotly-annotation-font-bold) also helps. – Quinten Oct 10 '22 at 14:09
0

This was what I was looking for, but it don't produce the same result as expected when I run it - the text didn't show. I edited and tested and got the text to show. But I don't understand all the arguments within the list, could you explain?

Here are some of them I think got figured out.

  • xref and yref: "paper" - suggest some ref. to the plot-area (but from where, i.e. what point I don't know)
  • x and y - moving the text in decimal %(?) from some point, increasing x - right, increasing y - up
  • showarrow - is self-explanitory

Thanks!

Christian
  • 117
  • 1
  • 3
  • 10