如何使用R在plot_ly中为子图提供字幕如何使用、中为、字幕、plot_ly

2023-09-06 06:56:07 作者:空山梦

我想知道如何使用 plot_ly 为子图提供不同的字幕.请有任何提示.在这种情况下,我得到了一个头衔 BB.谢谢.

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
) 

推荐答案

layout中的title属性是指整个绘图表面的标题,所以只能有一个.但是,我们可以使用文本注释为您的子图创建标题",例如:

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'))
)