r - 在 R 中创建绘图图表以在计数和百分比之间切换

标签 r plotly ggplotly

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

我一点也不擅长绘图,这就是我使用 ggplotly() 的原因(我的真实示例更复杂,ggplolty() 效果很好! ) 我想知道是否必须手动将图例添加到 % 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 = "%")
          )
        )
      )
    )

enter image description here

enter image description here

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

最佳答案

@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

enter image description here enter image description here

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

关于r - 在 R 中创建绘图图表以在计数和百分比之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480603/

相关文章:

callback - Dash Plotly 在两个页面中共享回调输入

python - 在绘图破折号中更新回调之外的应用程序状态

r - 将 ggplot 对象转换为 plotly 对象会创建与刻度值重叠的轴标题

r - 如何防止 Plotly 中的散点淡出?

r - 从 R 中的 CSV 文件创建 5 向维恩图

css - 使用来自 shinydashboard 的 infoBox 进入 shiny

r - 在自定义函数中应用 dist 函数 rowise

r - 在 dplyr 的 mutate 中使用 switch 语句

python - 模块未找到错误: No module named 'plotly'

r - 为什么 ggplotly 在 rmarkdown 中不能像 ggplot 一样工作