Fatima A
Fatima A

Reputation: 3

How can I add a caption to an R plotly scatterplot?

I'm trying to add a caption to a basic plot_ly to include more information about the scatter plot I have.

I've had a look at this thread: plotly adding a source or caption to a chart but the caption here is inline with the x-axis title.

I can't seem to get the caption underneath the plot and x-axis title.

Upvotes: 0

Views: 3768

Answers (1)

emilliman5
emilliman5

Reputation: 5966

Starting with the post you link to, you need to set the margin in the plot_ly layout and then change the x, and y in the annotations

library(plotly)
plot_ly(x=~hp, y=~mpg, data=mtcars, type="scatter", mode="marker") %>% 
  layout(margin = list(b=160), ##bottom margin in pixels
         annotations = 
           list(x = 0.5, y = -0.75, #position of text adjust as needed 
                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

Upvotes: 3

Related Questions