【问题标题】:How to give subtitles for subplot in plot_ly using R如何使用R在plot_ly中为子图提供字幕
【发布时间】:2016-09-14 02:53:46
【问题描述】:

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


    【解决方案1】:

    layout中的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'))
    )
    

    【讨论】:

      【解决方案2】:

      您现在可以利用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)
      

      【讨论】:

      • 我希望这将很快得到解决。在 R 中绘制动态数量的子图时,手动必须根据列和行的 nr 对每个可能的场景进行编码变得非常困难......
      • 最近在github上的开发版中修复了。
      【解决方案3】:

      我能够在 subplot() 上而不是在 plot_ly 对象本身上使用 layout(annotations()) 方案。这为动态可视化提供了更好的位置。所以要修改@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))`. 
      

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

      【讨论】:

        猜你喜欢
        • 2016-09-03
        • 2020-12-10
        • 1970-01-01
        • 2018-10-03
        • 2018-12-19
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多