【问题标题】:Plotly R : Using colour and transformation with a line plotPlotly R:使用颜色和转换线图
【发布时间】: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' & 按年份着色,同时保持物种过滤。 :)

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    看起来很奇怪,但是如果您首先按颜色变量重新排序 data.frame,您的代码就可以正常工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2019-01-28
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2021-10-31
      相关资源
      最近更新 更多