How to give subtitles for subplot in plot_ly
Asked Answered
O

3

29

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
) 
Overpower answered 17/5, 2016 at 20:29 Comment(0)
F
25

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'))
)
Ferryman answered 18/5, 2016 at 16:10 Comment(0)
I
28

Instead of positioning "by hand" (i.e., @d-roy's answer), you can now leverage subplot()'s ability to reposition paper referenced things like annotations (as well as shapes, images, etc).

library(plotly)
library(dplyr)

my_plot <- . %>% 
  plot_ly(x = ~date, y = ~value) %>%
  add_annotations(
    text = ~unique(variable),
    x = 0.5,
    y = 1,
    yref = "paper",
    xref = "paper",
    xanchor = "middle",
    yanchor = "top",
    showarrow = FALSE,
    font = list(size = 15)
  )

economics_long %>%
  group_by(variable) %>%
  do(p = my_plot(.)) %>%
  subplot(nrows = NROW(.), shareX = TRUE)
Impalpable answered 28/3, 2018 at 1:31 Comment(4)
I hope this will be fixed soon. When plotting a dynamic number of subplots in R, it becomes very tough to then manually have to code each possible scenario in terms of nr of cols and rows....Januaryjanuisz
It was recently fixed in the development version on github.Impalpable
@Impalpable What was the GitHub fix? I'm still unable to pass layout(title=) to plotly objects within subplots and have them only apply to that plotTownsend
@Impalpable , great answer! There is just one typo: xanchor = "middle" is not recognized and should be replaced by xanchor = "center"Vociferous
F
25

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'))
)
Ferryman answered 18/5, 2016 at 16:10 Comment(0)
B
6

I was able to use layout(annotations()) scheme not on the subplot() but on the plot_ly objects themselves. This gives a slightly better placement for dynamic visualization. So to rework @d-roy's answer:

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

Please note that in this case coordinates of the annotations are the same for each annotation because they are referring to each subplot and not the combined plot as a whole.

Beget answered 3/1, 2019 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.