如何在plot_ly中为子图提供字幕

问题描述 投票:0回答:3

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

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
) 
r plotly subplot subtitle
3个回答
28
投票

您现在可以利用

subplot()
的功能来重新定位纸张引用的内容,例如注释(以及形状、图像等),而不是“手动”定位(即 @d-roy 的答案)。

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)

25
投票

布局中的

title
属性指的是整个绘图面的标题,因此只能有一个。但是,我们可以使用文本注释为您的子图创建“标题”,例如:

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

6
投票

我能够不在 subplot() 上使用布局(annotations()) 方案,而是在plot_ly 对象本身上使用。这为动态可视化提供了稍微更好的位置。所以要修改@d-roy的答案:

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))`. 

请注意,在这种情况下,每个注释的注释坐标都是相同的,因为它们指的是每个子图,而不是整个组合图。

© www.soinside.com 2019 - 2024. All rights reserved.