代码如下:

{r, knitr::opts_chunk$set(echo = TRUE)}
library(plotly)
library(tidyverse)
library(magrittr)


#sample data
result_table = structure(list(wave = c(5L, 5L, 5L, 6L, 6L, 6L), term = c(1L, 
       2L, 3L, 1L, 2L, 3L), estimate = c(3.317, 0.887, 1, 0.828, 0.995, 
       1), Std.Error = c(1.249, 1.044, 1, 1.04, 1.003, 1), Statistic = c(10.305, 
       -1.571, 1, -3.297, -0.96, 1), P.Value = c(1, 1.01, 1, 1, 1.05, 
       1), conf.low = c(2.829, 0.802, 1, 0.749, 0.989, 1), conf.high = c(3.806, 
       0.973, 1, 0.906, 1, 1)), class = "data.frame", row.names = c(NA, 
       -6L))
result_table$wave %<>% as.factor
result_table$term %<>% as.factor
gg <- ggplot(result_table, aes(x = wave,  y = estimate,group = term)) +
      geom_point(aes(color = wave), size = 2) +
      geom_errorbar(aes(ymin=conf.low, ymax=conf.high,color = wave), 
                        linewidth=.1) + 
      geom_hline(yintercept = 0) + 
      labs(color = "Wave") +
      ylab('Estimate') +
      xlab('Term') +
      theme_classic() 

# Create a plotly object from ggplot
p <- ggplotly(gg)

p <- p %>% layout(showlegend = F,
  updatemenus = list(
    list(
      x = 1.5,
      y = 0.8,
#      yanchor = "bottom",
#      xanchor = 'center',
      buttons = list(
        list(method = "restyle",
             args = list("visible", list(TRUE, FALSE, FALSE)),
             label = "1"),
        
        list(method = "restyle",
             args = list("visible", list(FALSE, TRUE, FALSE)),
             label = "2"),
        
        list(method = "restyle",
             args = list("visible", list(FALSE, FALSE, TRUE)),
             label = "3")
      )
    )
  )
)

p

The result is : enter image description here

在图中,我想把变量term作为一个列表,这样我就可以 Select term中的项目并立即更新图.然而,plotly绘制所有的项目在term在同一时间,我可以做些什么来解决这个问题?

我用了 Add dropdown filter list to ggplot in RMarkdown HTML file WITHOUT using shiny?, 为什么他的代码可以很好地工作,而我的代码却不能很好地工作?

是因为geom_errorbar吗?

推荐答案

正如@M已经指出的--在 comments 中,plotly人在痕迹中思考.而要使您的过滤器工作,需要分别 for each term.我试着用groupAES解决这个问题,但失败了.相反,获得单独轨迹的一种 Select 是通过过滤数据 for each 值term分别添加geom层.为了避免重复代码,我使用了lapply.以这种方式,层或道被添加为每term 4条道的组,即前四条道用于term=1,依此类推.因此,当您在updatemenus中设置轨迹的可见性时,也必须考虑到这一点:

library(plotly)

gg <- ggplot(result_table, aes(
  x = wave,
  y = estimate,
  color = wave,
  group = term
)) +
  lapply(levels(result_table$term), \(x) {
    list(
      geom_point(data = ~ subset(.x, term == x), size = 2),
      geom_errorbar(
        data = ~ subset(.x, term == x),
        aes(ymin = conf.low, ymax = conf.high),
        linewidth = .1
      )
    )
  }) +
  geom_hline(yintercept = 0) +
  labs(color = "Wave") +
  ylab("Estimate") +
  xlab("Term") +
  theme_classic()

# Create a plotly object from ggplot
p <- ggplotly(gg)

library(plotly)

p <- p %>% layout(
  showlegend = F,
  updatemenus = list(
    list(
      x = 1.5,
      y = 0.8,
      buttons = list(
        list(
          method = "restyle",
          args = list(
            "visible",
            c(rep(TRUE, 4), rep(FALSE, 4), rep(FALSE, 4), TRUE)
          ),
          label = "1"
        ),
        list(
          method = "restyle",
          args = list(
            "visible",
            c(rep(FALSE, 4, 4), rep(TRUE, 4), rep(FALSE, 4), TRUE)
          ),
          label = "2"
        ),
        list(
          method = "restyle",
          args = list(
            "visible",
            c(rep(FALSE, 4, 4), rep(FALSE, 4), rep(TRUE, 4), TRUE)
          ),
          label = "3"
        )
      )
    )
  )
)

p

enter image description here

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

使用rlang s arg_match判断函数输入列表

使用对管道内单元格的引用生成新变量

在R底座中更改白天和夜晚的背景 colored颜色

从gtsummary包中使用tBL_strata()和tBL_summary()时删除变量标签

为什么观察不会被无功值变化触发?

是否可以创建一个ggplot与整洁判断的交互作用

如何使用tryCatch执行语句并忽略警告?

`lazy_dt`不支持`dplyr/across`?

用两种 colored颜色 填充方框图

R中有约束的优化问题:如何用复数和对数效益函数解决问题?

有没有可能用shiny 的书签恢复手风琴面板?

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

如何使用前缀作为匹配来连接数据帧?

在gggraph中显示来自不同数据帧的单个值

优化从每个面的栅格中提取值

删除数据帧中特定行号之间的每第三行和第四行

在不对R中的变量分组的情况下取两行的平均值

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

把代码写成dplyr中的group_by/摘要更简洁吗?