【问题标题】:Create plotly chart in R to toggle between count and percentage在 R 中创建绘图图表以在计数和百分比之间切换
【发布时间】:2020-08-12 06:58:03
【问题描述】:

一般的想法是创建一个可以在显示计数和百分比之间切换的绘图图表。我可以使用 updatemenu 更改显示的轨迹,并且轴刻度可以动态更改,但是当我在下面的示例中切换到“%”时,图例消失了。

我根本不是阴谋专家,这就是我使用ggplotly() 的原因(我的真实示例更复杂,ggplolty() 效果很好!)我想知道是否必须手动添加图例to % traces (3, 4) 在第一条迹线不可见时显示图例?

library(ggplot2)
library(plotly)

df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a", 
"b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433, 
0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125, 
0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L, 
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
row.names = c(NA, -10L))

p <- ggplot(df, aes(day, n, color = outcome)) +
  geom_line() +
  geom_line(aes(y = pct))

ggplotly(p, dynamicTicks = TRUE) %>% 
  style(visible = FALSE, traces = 3:4) %>% 
  layout(
    updatemenus = list(
      list(
        buttons = list(
          list(args = list("visible", list(TRUE, TRUE, FALSE, FALSE)),
               label = "n"),

          list(args = list("visible", list(FALSE, FALSE, TRUE, TRUE)),
               label = "%")
          )
        )
      )
    )

注意:这也发布在RStudio Community,但尚未收到任何答复。

【问题讨论】:

    标签: r plotly ggplotly


    【解决方案1】:

    @mfherman - 这是我想出的:

    library(ggplot2)
    library(plotly)
    library(scales)
    
    df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a", 
                                     "b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
                                     ), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433, 
                                                0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125, 
                                                0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L, 
                                                                                                       2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
                    row.names = c(NA, -10L))
    
    p <- ggplot(df, aes(day, n, color = outcome)) +
      geom_line() +
      geom_line(aes(y = pct)) + 
      theme(legend.position="bottom", legend.box = "horizontal")
    
    
    
    chart_type <- list(
      type = "buttons",
      direction = "right",
      xanchor = 'center',
      yanchor = "top",
      x = 0.5,
      y = 1.27,
      buttons = list(
        list(
          label = "nvals",
          method = "update",
          args = list( list("visible", list(TRUE, TRUE, FALSE, FALSE)), 
                       list( yaxis = list( range = c(0,200) ,
                                           ticksuffix = "") )
                      )),
        list(
            label = "%vals",
            method = "update",
            args = list( list("visible", list(FALSE, FALSE, TRUE, TRUE)), 
                         list( yaxis = list( range = c(0,100) ,
                                             ticksuffix = "%") )
            ))
      ))
    
    # https://plotly.com/r/custom-buttons/#relayout-button
    
    
    
    p2 <- ggplotly(p, dynamicTicks = TRUE, width = 640, height = 420) %>% 
      style(visible = FALSE, traces = 3:4) %>% 
      layout(
          legend = list(orientation = "h",y = 0.8, x = 0.8),
          updatemenus = list( chart_type )
    )
    
    
    for (i in 1:ncol(df)){
      p2$x$data[[i]]$showlegend <- TRUE
    }
    p2
    

    重要的部分在底部 -> 看起来 p2$x$data[[i]]$showlegend 值在第二张图表上默认设置为 false。可能值得在 github 项目上作为问题打开,以便将其添加为布局列表中的选项。看起来他们现在只有hide_legend 选项……很奇怪。

    https://github.com/ropensci/plotly/issues

    感谢这个问题帮助我解决这个问题:

    https://github.com/ropensci/plotly/issues/842

    编辑:根据评论中的要求添加了 % yaxis

    【讨论】:

    • 就是这样,谢谢!一个切线问题是如何在选择下拉菜单时将 y 轴标签更改为 % 格式。看起来您可以在一个按钮中指定布局更改,但我还不能让它在一个按钮中同时更改布局和样式。
    • 为您添加了 % yaxis 并为您清理了按钮列表方法。学会了很多关于情节做这个练习。
    猜你喜欢
    • 2020-03-22
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多