【发布时间】:2021-01-02 11:34:17
【问题描述】:
我希望按组(花卉种类)和年份(我制作的虚假数据)进行过滤。但是,当我尝试这样做时,过滤会中断。下图说明了这一点,其中 setosa 被采摘,但地块上有Virgincia 数据。它看起来是由使用color = 引起的。我为这个问题提供了一个解决方案,但是,在我的真实示例中,我的mode = 'lines'。这不允许我使用此解决方案。
就上下文而言,我的真实示例是 Duck 曲线图。其中类别是国家,x 轴是一天中的小时。
可重现的例子:
set.seed(42)
iris$year <- sample(2009:2011, 150, replace=TRUE) #MAKING FAKE YEAR DATA
p <- iris %>%
plot_ly(
type = 'scatter',
x = ~Sepal.Length,
y = ~Petal.Length,
color = ~as.factor(year), # GROUPING BY YEAR
####### SAMPLE SOLUTION ######## comment out color =. and uncomment the below
# marker = list(color = ~as.factor(year), size = 8, opacity = 0.8),
text = ~Species, # ADDED THIS TO ILLUSTRATE THERE IS WRONG DATA IN THE TRANSFORMATION
mode = 'markers', # IN MY REAL EXAMPLE IM USING LINES.
transforms = list(
list(
type = 'filter',
target = ~Species,
operation = '=',
value = unique(iris$Species)[1]
)
)) %>% layout(
updatemenus = list(
list(
type = 'dropdown',
active = 0,
buttons = list(
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[1]),
label = unique(iris$Species)[1]),
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[2]),
label = unique(iris$Species)[2]),
list(method = "restyle",
args = list("transforms[0].value", unique(iris$Species)[3]),
label = unique(iris$Species)[3])
)
)
)
)
p
预期解决方案:mode = 'lines' & 按年份着色,同时保持物种过滤。 :)
【问题讨论】: