Plotly R:使用颜色和线图变换

时间:2020-09-15 16:48:45

标签: r plotly r-plotly

我希望按组(花朵种类)和年份(我制作的假数据)进行过滤。但是,当我尝试这样做时,过滤会中断。下图显示了采摘setosa的图片,但是该地块上有弗吉尼亚数据。它似乎是由于使用cp: missing file operand Try 'cp --help' for more information. 引起的。但是,在我的真实示例中,我提供了一个解决方案color =。哪个不允许我使用此解决方案。

对于上下文,我的真实示例是鸭曲线图。其中类别为国家/地区,x轴为一天中的小时。

可复制的示例:

mode = 'lines'

enter image description here

预期的解决方案: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 和按颜色分类,同时保持对物种的过滤。 :)

1 个答案:

答案 0 :(得分:1)

似乎很奇怪,但是如果您首先通过颜色变量对data.frame进行重新排序,则代码可以正常工作。

set.seed(42)
iris$year <- sample(2009:2011, 150, replace=TRUE) #MAKING FAKE YEAR DATA
iris <- iris[order(iris$year), ]