rplotlysubplotsubtitle

How to give subtitles for subplot in plot_ly


I am wondering how to give difference subtitles for the subplots using plot_ly. Any hint please. I got one title BB in this case. Thanks.

p <- subplot(
      plot_ly(economics, x = date, y = uempmed)%>%layout(showlegend = FALSE, title="AA"),
      plot_ly(economics, x = date, y = unemploy)%>%layout(showlegend = FALSE, title="BB"),
margin = 0.05
) 

Solution

  • The title attribute in layout refers to the title for the entire plotting surface, so there can only be one. However, we can use text annotations to create "titles" for your subplots, for example:

    p <- subplot(
      plot_ly(economics, x = date, y = uempmed)%>%layout(showlegend = FALSE),
      plot_ly(economics, x = date, y = unemploy)%>%layout(showlegend = FALSE),
      margin = 0.05
    ) 
    p %>% layout(annotations = list(
     list(x = 0.2 , y = 1.05, text = "AA", showarrow = F, xref='paper', yref='paper'),
      list(x = 0.8 , y = 1.05, text = "BB", showarrow = F, xref='paper', yref='paper'))
    )