在R中创建在计数和百分比之间切换的plotly图表。

问题描述 投票:1回答:1

总的想法是创建一个可以在计数和百分比之间切换的plotly图表。我可以使用updatemenu更改显示哪些轨迹,轴的ticks也可以动态变化,但是当我在下面的例子中切换到"%"时,图例消失了。

我并不是一个专业的绘图人员,这就是为什么我在使用 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社区但没有收到任何答复。

r plotly ggplotly
1个回答
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.comropensciplotlyissues)。

感谢这个问题帮我解决了这个问题。

https:/github.comropensciplotlyissues842。

enter image description hereenter image description here

编辑:按照评论中的要求添加了%y轴。

© www.soinside.com 2019 - 2024. All rights reserved.