更新时间:2022-10-14 23:26:47
对于第一个示例,作为您在数据集***享的第一个链接,它们具有 frame
列,而 frame
plotly 函数中的参数来创建动画.
您创建的数据集没有 frame
变量,但是您仍然在 plotly
调用中使用 frame
,这导致了错误./p>
和 frame
变量是使用示例链接中定义的 accumulate_by
函数生成的.对于每个日期框架记录,都有该日期之前所有日期的重复记录.因此,如果您的数据具有 1,462
条记录,则在 accumulate_by
函数创建的用于绘图的动画上构建的数据具有 535,092
条记录
这是第一个示例的工作代码:
[已更新]-将x轴更改为十进制数字,其中日期为小数点.
数据<-数据%>%mutate(date_decision_made = as.Date(date_decision_made,format =%Y/%m/%d"),tmp_date = lubridate :: year(date_decision_made)+as.numeric(strftime(date_decision_made,format =%j"))/365)accumulate_by<-函数(dat,var){var<-lazyeval :: f_eval(var,dat)lvls<-plotly ::: getLevels(var)数据<-lapply(seq_along(lvls),function(x){cbind(dat [var%in%lvls [seq(1,x)],],frame = lvls [[x]])})dplyr :: bind_rows(日期)}#动画片数据<-数据%>%accumulate_by(〜tmp_date)图<-数据%>%plot_ly(x =〜tmp_date,y =〜property_damages_in_dollars,split =〜class,框架=〜框架,类型='散点图',模式='线',行=列表(simplyfy = F))图
关于示例2中的代码,您错过了一次关闭()
.在将 updatemenus
的分配包含在 list
函数中之后,此错误使一切都变得正常,最终导致您得到错误.
updatemenus<-列表(列表(活动= -1,type ='buttons',按钮=列表(列表(标签="time_series_1",方法=更新",args = list(list(visible = c(FALSE,TRUE)),列表(标题=系列1",注解= list(c(),high_annotations)))),列表(标签="time_series_2",方法=更新",args = list(list(visible = c(TRUE,FALSE)),列表(标题=系列2",注解= list(low_annotations,c())))),))#你错过了这个)图<-数据%>%plot_ly(type ='scatter',mode ='lines')无花果<-无花果%>%add_lines(x =〜date_decision_made,y =〜property_damages_in_dollars,名称=高",行=列表(颜色=#33CFA5"))无花果<-无花果%>%add_lines(x =〜date_decision_made,y =〜property_damage_in_dollars,名称=低",行=列表(颜色=#F06A6A"))图<-图%>%布局(标题="Apple",showlegend = FALSE,xaxis = list(title ="Date"),yaxis = list(title =价格($)")),updatemenus = updatemenus)图
I am using the R programming language. I found some examples on the plotly r website that I am trying to replicate: https://plotly.com/r/cumulative-animations/ and https://plotly.com/r/custom-buttons/
I created some artificial time series data:
library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
#create data
#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
property_damages_in_dollars <- rnorm(731,100,10)
final_data <- data.frame(date_decision_made, property_damages_in_dollars)
final_data %>%
mutate(date_decision_made = as.Date(date_decision_made)) %>%
add_count(week = format(date_decision_made, "%W-%y"))
final_data$class = "time_series_1"
#time series 2
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
property_damages_in_dollars <- rnorm(731,10,10)
final_data_2 <- data.frame(date_decision_made, property_damages_in_dollars)
final_data_2 %>%
mutate(date_decision_made = as.Date(date_decision_made)) %>%
add_count(week = format(date_decision_made, "%W-%y"))
final_data_2$class = "time_series_2"
#combine
data = rbind(final_data, final_data_2)
I tried to follow the examples as close as I could with my data but I got the following errors:
#example 1
#animation
fig <- data %>%
plot_ly(
x = ~date_decision_made,
y = ~property_damages_in_dollars,
split = ~class,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
)
fig <- fig %>% layout(
xaxis = list(
title = "Date",
zeroline = F
),
yaxis = list(
title = "Median",
zeroline = F
)
)
fig <- fig %>% animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
)
fig <- fig %>% animation_slider(
hide = T
)
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
#Error in tr[["frame"]][[1]] : object of type 'closure' is not subsettable
And the second example:
#example 2:
# updatemenus component
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "time_series_1",
method = "update",
args = list(list(visible = c(FALSE, TRUE)),
list(title = "series 1",
annotations = list(c(), high_annotations)))),
list(
label = "time_series_2",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "series 2",
annotations = list(low_annotations, c() )))),
)
)
fig <- data %>% plot_ly(type = 'scatter', mode = 'lines')
fig <- fig %>% add_lines(x=~date_decision_made, y=~property_damages_in_dollars, name="High",
line=list(color="#33CFA5"))
fig <- fig %>% add_lines(x=~date_decision_made, y=~property_damage_in_dollars, name="Low",
line=list(color="#F06A6A"))
fig <- fig %>% layout(title = "Apple", showlegend=FALSE,
xaxis=list(title="Date"),
yaxis=list(title="Price ($)"),
updatemenus=updatemenus)
fig
# Error: unexpected symbol in:
"
fig"
Can someone please show me what I am doing wrong?
Thanks
For the first example, as the first link you share in the dataset they have frame
column which used by frame
param in plotly
function to create the animation.
The dataset you created didn't have frame
variable but you still using frame
in plotly
call which caused the error you got.
And frame
variable is generated using the accumulate_by
function defined in the example link. For every date frame record, there are duplicate records of all the date before that date. So if your data has 1,462
records then the data that built for animation on plotly that created by accumulate_by
function has 535,092
records
Here is the working code for the first example:
[Updated] - Change x Axis to decimal number where date is decimal point.
data <- data %>%
mutate(date_decision_made = as.Date(date_decision_made, format = "%Y/%m/%d"),
tmp_date = lubridate::year(date_decision_made) +
as.numeric(strftime(date_decision_made, format = "%j"))/365)
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)
}
#animation
data <- data %>% accumulate_by(~tmp_date)
fig <- data %>%
plot_ly(
x = ~tmp_date,
y = ~property_damages_in_dollars,
split = ~class,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
)
fig
Regard the code in example 2 you missed one close )
. This mistake made everything after the assignment of updatemenus
was included in the list
function and end up result the error you got.
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "time_series_1",
method = "update",
args = list(list(visible = c(FALSE, TRUE)),
list(title = "series 1",
annotations = list(c(), high_annotations)))),
list(
label = "time_series_2",
method = "update",
args = list(list(visible = c(TRUE, FALSE)),
list(title = "series 2",
annotations = list(low_annotations, c() )))),
)
)
# you missed this one
)
fig <- data %>% plot_ly(type = 'scatter', mode = 'lines')
fig <- fig %>% add_lines(x=~date_decision_made,
y=~property_damages_in_dollars, name="High",
line=list(color="#33CFA5"))
fig <- fig %>% add_lines(x=~date_decision_made,
y=~property_damage_in_dollars, name="Low",
line=list(color="#F06A6A"))
fig <- fig %>% layout(title = "Apple", showlegend=FALSE,
xaxis=list(title="Date"),
yaxis=list(title="Price ($)"),
updatemenus=updatemenus)
fig