r - 情节不显示
问题描述
我正在尝试为这个测试制作动画,data.frame
但plotly
情节甚至没有出现!不过,相同的代码适用于原始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"
)
解决方案
您在示例中忽略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 "
)
)
推荐阅读
- php - AWS Bitnami LAMP 服务器上的 CakePHP 无法连接到数据库
- azure - 适用于 WebApp 的 Azure NetworkSecurityGroup 规则
- android - 检查结果中未显示 Android Studio 警告
- c# - 发布到 SmarterASP.net 时构建失败
- docker - 在部署 Docker 容器(在 Kubernetes 中)时置换静态目录结构的最佳实践?
- powershell - 如何创建一个 powershell 脚本,该脚本将提供用户指定目录的目录列表?
- python - PyArmor - 检索许可证信息
- c++ - 如何从外部文件中选择一个随机词到类?
- python - 误解 OpenGL 中的 Model-View-Projection 矩阵
- vbscript - 创建文件时 VBScript 中出现“找不到路径”错误