如何将 xts 对象列表打印到单个时间序列图中?
How to print a list of xts objects into a single time series plot?
我想绘制一长串时间序列对象的叠加。
class(ts.l)
# [1] "list"
class(ts.l[[1]])
# [1] "xts" "zoo"
我知道 ts.plot()
可以做到。所以,当我调用命令 ts.plot(as.ts(ts.l[[1]]), as.ts(ts.l[[2]]), as.ts(ts.l[[2]]))
时,它起作用了。
但我不知道如何将整个列表一次全部提供给 ts.plot()
。以下代码行都不起作用:
ts.plot(unlist(sapply(ts.l, as.ts)))
这似乎将时间序列合并为一个序列。
ts.plot(sapply(ts.l, as.ts))
生成错误消息
Object cannot be coerced to type double
那么,有人知道如何处理吗?
我使用以下函数将 xts
对象转换为 ggplot2
可以使用的格式:
xts_to_dataframe = function(..., xts_ids) {
xts_list = list(...)
all_include_stop_time = all(sapply(xts_list, function(x) return(!is.null(attr(x, 'stop_timestamps')))))
names_null = sapply(xts_list, function(x) is.null(names(x)))
if (any(names_null)) stop('Some xts objects do not have a valid `names` attribute.')
data_frame_list = lapply(xts_list, function(xts_obj) {
xts_vars = lapply(names(xts_obj), function(xts_variable) {
output = data.frame(timestamps = index(xts_obj[,xts_variable]),
values = as.vector(xts_obj[,xts_variable]))
if (all_include_stop_time) {
output = cbind(output, stop_timestamps = attr(xts_obj, 'stop_timestamps'))
}
return(output)
})
combined_data_frame = do.call('rbind', xts_vars)
rownames(combined_data_frame) = NULL
combined_data_frame[['variable']] = rep(names(xts_obj), sapply(xts_vars, nrow))
return(combined_data_frame)
})
combined_data_frame_list = do.call('rbind', data_frame_list)
if (missing(xts_ids)) xts_ids = seq_along(data_frame_list)
combined_data_frame_list[['xts_obj_id']] = rep(xts_ids, sapply(data_frame_list, nrow))
return(combined_data_frame_list)
}
只需给它提供一堆 xts
个对象,然后可以使用 ggplot2
绘制生成的 data.frame
。例如:
ggplot(plot_data, aes(x = timestamps, y = values, color = xts_obj_id)) + geom_point()
development version of xts on GitHub 中的新 plot.xts
处理 multi-column xts 对象。例如:
require(xts)
data(sample_matrix)
# create list of xts objects
ts.l <- as.list(as.xts(sample_matrix))
# merge xts list into one object
ts.o <- do.call(merge, ts.l)
plot(ts.o)
我想绘制一长串时间序列对象的叠加。
class(ts.l)
# [1] "list"
class(ts.l[[1]])
# [1] "xts" "zoo"
我知道 ts.plot()
可以做到。所以,当我调用命令 ts.plot(as.ts(ts.l[[1]]), as.ts(ts.l[[2]]), as.ts(ts.l[[2]]))
时,它起作用了。
但我不知道如何将整个列表一次全部提供给 ts.plot()
。以下代码行都不起作用:
ts.plot(unlist(sapply(ts.l, as.ts)))
这似乎将时间序列合并为一个序列。
ts.plot(sapply(ts.l, as.ts))
生成错误消息
Object cannot be coerced to type double
那么,有人知道如何处理吗?
我使用以下函数将 xts
对象转换为 ggplot2
可以使用的格式:
xts_to_dataframe = function(..., xts_ids) {
xts_list = list(...)
all_include_stop_time = all(sapply(xts_list, function(x) return(!is.null(attr(x, 'stop_timestamps')))))
names_null = sapply(xts_list, function(x) is.null(names(x)))
if (any(names_null)) stop('Some xts objects do not have a valid `names` attribute.')
data_frame_list = lapply(xts_list, function(xts_obj) {
xts_vars = lapply(names(xts_obj), function(xts_variable) {
output = data.frame(timestamps = index(xts_obj[,xts_variable]),
values = as.vector(xts_obj[,xts_variable]))
if (all_include_stop_time) {
output = cbind(output, stop_timestamps = attr(xts_obj, 'stop_timestamps'))
}
return(output)
})
combined_data_frame = do.call('rbind', xts_vars)
rownames(combined_data_frame) = NULL
combined_data_frame[['variable']] = rep(names(xts_obj), sapply(xts_vars, nrow))
return(combined_data_frame)
})
combined_data_frame_list = do.call('rbind', data_frame_list)
if (missing(xts_ids)) xts_ids = seq_along(data_frame_list)
combined_data_frame_list[['xts_obj_id']] = rep(xts_ids, sapply(data_frame_list, nrow))
return(combined_data_frame_list)
}
只需给它提供一堆 xts
个对象,然后可以使用 ggplot2
绘制生成的 data.frame
。例如:
ggplot(plot_data, aes(x = timestamps, y = values, color = xts_obj_id)) + geom_point()
development version of xts on GitHub 中的新 plot.xts
处理 multi-column xts 对象。例如:
require(xts)
data(sample_matrix)
# create list of xts objects
ts.l <- as.list(as.xts(sample_matrix))
# merge xts list into one object
ts.o <- do.call(merge, ts.l)
plot(ts.o)