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


我想知道如何使用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
) 

5886
2018-05-17 20:29


起源



答案:


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

13
2018-05-18 16:10





您现在可以利用,而不是“手动”定位(即@ d-roy的答案) subplot()能够重新定位纸张引用的东西,如注释(以及形状,图像等)。

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)

2
2018-03-28 01:31



我希望很快就会解决这个问题。在R中绘制动态数量的子图时,手动必须按照col和行的nr编码每个可能的场景变得非常困难.... - Mark
它最近在github的开发版本中修复。 - Carson


答案:


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

13
2018-05-18 16:10





您现在可以利用,而不是“手动”定位(即@ d-roy的答案) subplot()能够重新定位纸张引用的东西,如注释(以及形状,图像等)。

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)

2
2018-03-28 01:31



我希望很快就会解决这个问题。在R中绘制动态数量的子图时,手动必须按照col和行的nr编码每个可能的场景变得非常困难.... - Mark
它最近在github的开发版本中修复。 - Carson