如何使用R为plot_ly提供子图的字幕

yux*_* zi 14 r subtitle subplot plotly

我想知道如何使用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
) 
Run Code Online (Sandbox Code Playgroud)

d-r*_*roy 14

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'))
)
Run Code Online (Sandbox Code Playgroud)


小智 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))`. 
Run Code Online (Sandbox Code Playgroud)

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


Car*_*son 5

您现在可以利用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)
Run Code Online (Sandbox Code Playgroud)

  • 我希望这会很快得到解决。在 R 中绘制动态数量的子图时,手动必须根据 cols 和 rows 的 nr 对每个可能的场景进行编码变得非常困难...... (2认同)