我想根据我的数据中的一列离散值来过滤用plotly
创建的图表。最终目标是能够使用按钮更新筛选器值,因此我不想预先过滤数据。
library(plotly)
df <- data.frame(group1 = rep(c('low', 'high'), each = 25),
x = rep(1:5, each = 5),
group2 = letters[1:5],
y = c(runif(25, 0, 2), runif(25, 3, 5)))
plot_ly(df, x = ~x, y = ~y, type = 'scatter',
mode = 'line',
color = ~group2,
transforms = list(
list(
type = 'filter',
target = ~group1,
operation = '=',
value = 'high'
)
)
)
我预计这会给出下面的图表:
但它给出了这样的结果:
它似乎过滤了错误的变量。为什么数据没有按照我期望的方式进行过滤?
发布于 2018-09-18 08:17:40
问题似乎在于transforms
列表的target
参数只能接受plot_ly属性的名称,而不能接受原始数据。下面的代码可以工作:
library(plotly)
set.seed(1)
df <- data.frame(group1 = rep(c("low", "high"), each = 25),
x = rep(1:5, each = 5),
group2 = letters[1:5],
y = c(runif(25, 0, 2), runif(25, 3, 5)))
plot_ly(df, x = ~x, y = ~y, customdata=~group1, type = 'scatter',
mode = 'line',
color = ~group2,
transforms = list(
list(
type = 'filter',
target = 'customdata',
operation = '=',
value = 'high'
)
)
)
并生成与以下内容相同的图表
plot_ly(df %>% filter(group1=="high"), x = ~x, y = ~y, type = 'scatter',
mode = 'line',
color = ~group2
)
当然,您可以用ids
等替换它,而不是使用customdata
,这是plot_ly
允许的属性,但不会影响图表的美观。
https://stackoverflow.com/questions/52321695
复制相似问题