R:绘制时间序列图
我正在使用R编程语言。我在plotly r网站上找到了一些我试图复制的例子:和 我创建了一些人工时间序列数据:R:绘制时间序列图,r,dplyr,time-series,plotly,data-visualization,R,Dplyr,Time Series,Plotly,Data Visualization,我正在使用R编程语言。我在plotly r网站上找到了一些我试图复制的例子:和 我创建了一些人工时间序列数据: 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"
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)
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
#创建数据
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)
日期决策对于第一个示例,作为数据集中共享的第一个链接,它们有frame
列,由plotly
函数中的frame
param使用该列创建动画
您创建的数据集没有frame
变量,但您仍在plotly
调用中使用frame
,这导致了出现错误
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
而frame
变量是使用示例链接中定义的acculate\u by
函数生成的。对于每个日期框记录,都有该日期之前所有日期的重复记录。因此,如果您的数据有1462
记录,则为动画而构建的数据(由acculate\u by
函数创建)有535092
记录
以下是第一个示例的工作代码:
[更新]-将x轴更改为十进制数,其中日期为小数点。
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
定义函数后,您在哪里使用该函数?谢谢您的回复!我又看了一眼,似乎没有使用acculate_by()函数?没错。您已经定义了它,但从未使用过它。再往下看,你试图使用一个变量,我希望这个变量在使用之前已经被函数处理过了,所以我怀疑这可能是问题的一部分。在应用这个函数之前,我真的不知道。是的,这就是我的意思。谢谢你的回答!我现在正在运行代码,由于某些原因,它需要很长时间才能运行:)关于第二个示例,当我运行代码时,它告诉我“错误:找不到对象‘high_annotations’”。你知道这是为什么吗?谢谢你的帮助!我真的很感激(我在这里发布了一个相关的问题:)@stats555-我更新了十进制数字的答案,其中日期是小数点,这样X轴将显示在2014年、2015年,看起来像是一年,而不是以前。请注意,plotly动画可以很好地使用X轴是数字,当它不是数字时,它似乎不能很好地工作…您不能只是复制脚本并运行-我自己无法运行代码,因为代码中使用了很多变量,但没有定义。我发现了代码中的语法错误,因为它遗漏了)
,但还有很多地方需要改进<代码>“高注释”未找到
是因为没有定义。。。请回到您复制代码的地方,仔细阅读
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
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