R:绘图时间序列图 [英] R: plotly time series graphs
问题描述
我正在使用R编程语言.我在我想复制的plotly r网站上找到了一些示例: https://plotly.com/r/cumulative-animations/和 https://plotly.com/r/custom-buttons/
我创建了一些人工时间序列数据:
库(xts)库(ggplot2)图书馆(dplyr)图书馆(密谋)#创建数据#时间系列1date_decision_made = seq(as.Date("2014/1/1"),as.Date("2016/1/1"),by ="day")date_decision_made<-格式(as.Date(date_decision_made),%Y/%m/%d")property_damages_in_dollars<-诺尔姆(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(星期=格式(date_decision_made,%W-%y")))final_data $ class ="time_series_1"#时间系列2date_decision_made = seq(as.Date("2014/1/1"),as.Date("2016/1/1"),by ="day")date_decision_made<-格式(as.Date(date_decision_made),%Y/%m/%d")property_damages_in_dollars<-诺尔姆(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(星期=格式(date_decision_made,%W-%y")))final_data_2 $ class ="time_series_2";#结合数据= rbind(final_data,final_data_2)
我尝试尽可能地遵循示例处理数据,但是出现以下错误:
#example 1#动画片图<-数据%>%plot_ly(x =〜date_decision_made,y =〜property_damages_in_dollars,split =〜class,框架=〜框架,类型='散点图',模式='线',行=列表(simplyfy = F))图<-图%>%布局(xaxis =列表(标题=日期",零线= F),yaxis = list(标题=中位数",零线= F))图<-图%>%animation_opts(帧= 100,过渡= 0,重画= FALSE)图<-图%>%animation_slider(隐藏= T)图<-图%>%animation_button(x = 1,xanchor =右",y = 0,yanchor =下".)#tr [["frame"]] [[1]中的错误:类型为'closure'的对象不可子集化
第二个例子:
#example 2:#updatemenus组件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,name ="High",行=列表(颜色=#33CFA5"))图<-图%>%add_lines(x =〜date_decision_made,y =〜property_damage_in_dollars,name ="Low",行=列表(颜色=#F06A6A"))图<-图%>%布局(标题="Apple",showlegend = FALSE,xaxis = list(title ="Date"),yaxis = list(title =价格($)")),updatemenus = updatemenus)图#错误:中出现意外的符号:"无花果
有人可以告诉我我在做什么错吗?
谢谢
对于第一个示例,作为您在数据集中共享的第一个链接,它们具有 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
这篇关于R:绘图时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!