group-by 的串扰问题

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

我想知道是否可以获取(动态)过滤数据的方法,如下所示。 看来,即使我按时间和组进行分组,它也会在分组中添加键(在本例中为行号)。 所以结果并不是应该的... 有什么想法/替代方案(没有闪亮的)??

library(crosstalk)
library(tidyverse)
library(plotly)


set.seed(12345)
d <- data.frame(
  time = rep(0:9, 10),
  group = sample(paste0("Group_", seq(5)), size = 1000, replace = TRUE),
  value = rnorm(n = 1000) + 1
)

sd1=SharedData$new(d )


    fig =plot_ly(sd1) %>% group_by(time, group) %>% mutate(mm=mean(value)) %>% 
     add_trace(x = ~time, y = ~mm, type = 'scatter', mode = 'lines+markers', color = ~group)

# means by row rather than group    
    fig

# I want to use the fig with filters


bscols(
  widths = c(3,9),
  list(
  filter_slider("f1","value",sd1, ~value),
  filter_select("f2","time",sd1, ~time)
  ),
fig
  
)
r dplyr plotly crosstalk
1个回答
0
投票

我想我解决了它......我应该使用情节 - 变换:

fig <- plot_ly(
  data=sd1,
  type = 'scatter',
  color = ~group,
  y = ~value,
  x=~time,
  mode = 'markers',
  transforms = list(
    list(
      type = 'aggregate',
      groups =~ list(group,time),
      aggregations = list(
        list(
          target = 'y', func = 'mean', enabled = T
        )
      )
    )
  )
)
© www.soinside.com 2019 - 2024. All rights reserved.