首页 > 解决方案 > 情节不显示

问题描述

我正在尝试为这个测试制作动画,data.frameplotly情节甚至没有出现!不过,相同的代码适用于原始plotly数据。我已经仔细检查过column's class,它们与plotly示例相同。我现在很困惑为什么这会失败。

如您所见,这也适用于marker模式,但不适用于模式。lines

total <- data.frame(replicate(4,sample(0:1, 100, rep=TRUE)))
names(total) <- c("date", "frame", "P1.10", "year")
total$date <- as.numeric(as.character(t(rbind(runif(100, min=2000, max=2010)))))
f.rank <- order(total$date)
total$frame[f.rank] <- 1:nrow(total)
total$P1.10 <- as.numeric(as.character(t(rbind(runif(100, min=1, max=10)))))
total$year <- 2000



p <- total %>%
  plot_ly(
    x = ~date, 
    y = ~P1.10,
    frame = ~frame, 
    type = 'scatter',
    mode = 'lines', 
    line = list(simplyfy = F)
  ) %>% 
  layout(
    xaxis = list(
      title = "Date",
      zeroline = F
    ),
    yaxis = list(
      title = "P1.10",
      zeroline = F
    )
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) %>%
  animation_slider(
    hide = T
  ) %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )

标签: rplotly

解决方案


您在示例中忽略accumulate_by了。您还需要一个ID字段。这是相同的,但ggplot组合使用。

set.seed(123)
library(plotly)

total <- data.frame(replicate(4,sample(0:1, 100, rep=TRUE)))
names(total) <- c("date", "frame", "P1.10", "year")
total$date <- as.numeric(as.character(t(rbind(runif(100, min=2000, max=2010)))))
f.rank <- order(total$date)
total$frame[f.rank] <- 1:nrow(total)
total$ID[f.rank] <- 1:nrow(total)
total$P1.10 <- as.numeric(as.character(t(rbind(runif(100, min=1, max=10)))))
total$year <- 2000

accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
}

total <- total %>%
  accumulate_by(~ID)

p <- ggplot(total,aes(ID, P1.10, frame = frame)) +
  geom_line()

p <- ggplotly(p) %>%
  layout(
    title = "",
    yaxis = list(
      title = "P1.10",
      zeroline = F,
      tickprefix = "$"
    ),
    xaxis = list(
      title = "Date",
      zeroline = F, 
      showgrid = F
    )
  ) %>% 
  animation_opts(
    frame = 100, 
    transition = 0, 
    redraw = FALSE
  ) %>%
  animation_slider(
    currentvalue = list(
      prefix = "Day "
    )
  )

推荐阅读