Plotly R:使用颜色和变换进行线图绘制

Plotly R:使用颜色和变换进行线图绘制,r,plotly,r-plotly,R,Plotly,R Plotly,我希望按组(花卉种类)和年份(我制作的假数据)进行过滤。但是,当我尝试这样做时,过滤中断。下图显示了这一点,其中选择了setosa,但图中有virgincia数据。它似乎是由使用color=引起的。然而,在我的实际示例中,我提供了这个问题的解决方案,即mymode='lines'。这不允许我使用这个解决方案 在上下文中,我的真实示例是Duck曲线图。其中类别为国家,x轴为一天中的小时 可复制示例: set.seed(42) iris$year <- sample(2009:2011, 15

我希望按组(花卉种类)和年份(我制作的假数据)进行过滤。但是,当我尝试这样做时,过滤中断。下图显示了这一点,其中选择了setosa,但图中有virgincia数据。它似乎是由使用
color=
引起的。然而,在我的实际示例中,我提供了这个问题的解决方案,即my
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
set.seed(42)
iris$year%布局(
updatemenus=列表(
名单(
类型='dropdown',
活动=0,
按钮=列表(
列表(method=“restyle”,
args=list(“transforms[0].value”,唯一(iris$Species)[1]),
标签=唯一(虹膜$物种)[1]),
列表(method=“restyle”,
args=list(“transforms[0].value”,唯一(iris$Species)[2]),
标签=唯一(虹膜$物种)[2],
列表(method=“restyle”,
args=list(“transforms[0].value”,唯一(iris$Species)[3]),
标签=唯一(虹膜$物种)[3])
)
)
)
)
P


预期解决方案:
mode='lines'
&在保留物种过滤的同时按年份着色。:)

似乎有点奇怪,但如果您首先按颜色变量对data.frame重新排序,则代码工作正常

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

set.seed(42)
iris$年