首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >plot plot不显示

plot plot不显示
EN

Stack Overflow用户
提问于 2019-04-10 14:44:54
回答 1查看 136关注 0票数 4

我试图让这个测试data.frame具有动画效果,但是plotly图甚至都没有显示出来!不过,同样的代码也适用于原始plotly数据。我已经复选了column's class,它们和plotly的例子是一样的。我现在不明白为什么这样做会失败。

如您所见,这在marker模式下也有效,但在lines模式下不起作用。

代码语言:javascript
复制
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"
  )
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-10 16:06:47

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

代码语言:javascript
复制
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 "
    )
  )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55606286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档