可能在Plotly动画中改变帧之间的不透明度或颜色?

时间:2018-03-15 18:20:42

标签: r plotly

使用Plotly动画示例,这可以修改整体不透明度

library(plotly)
library(gapminder)

gap <- gapminder
gap$opa <- 0.5

p <- gap %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    opacity = ~opa,
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  ) %>%
  layout(
    xaxis = list(
      type = "log"
    )
  )

p

但我想要做的是在帧之间进行不透明度更改(或颜色),以便我可以在某个帧之前隐藏数据点,但这不起作用:

library(plotly)
library(gapminder)

gap <- gapminder
gap$opa <- ifelse(gap$year < 1970, 0, 1)

p <- gap %>%
  plot_ly(
    x = ~gdpPercap, 
    y = ~lifeExp, 
    size = ~pop, 
    color = ~continent, 
    frame = ~year, 
    opacity = ~opa,
    text = ~country, 
    hoverinfo = "text",
    type = 'scatter',
    mode = 'markers'
  ) %>%
  layout(
    xaxis = list(
      type = "log"
    )
  )

p

谢谢!

编辑*标题,应该说“可能”

1 个答案:

答案 0 :(得分:0)

您应该能够使用开始和结束年份的范围来创建列表,然后将不透明度设置为沿时间轴的距离。

start = 1952
end = 2007

list_length = length(seq(start, end))
opa <- vector(mode="list", length = list_length)
names(opa) <- c(seq(start, end))

# sets opacity between 0 and 1
for (year in c(seq(1, list_length))){
    opa[[year]] <- year / list_length
}

然后,您可以使用每个帧中传递的year变量来调用相应的不透明度。

> opa[['1952']]
[1] 0.01785714

我猜你在做这个例子:https://plot.ly/r/animations/