我有一个大型数据集,我想从中制作 3D 表面。我希望 x 轴是日期,y 轴是时间(24 小时),z 轴(高度)是我拥有的值($)。我是R的初学者,所以越简单越好!
http://www.quantmod.com/examples/chartSeries3d/有一个很好的例子,但代码对我的技能水平来说太复杂了!
任何帮助将不胜感激 - 到目前为止我研究过的任何内容都需要对数据进行排序,我认为这不合适。
最佳答案
有几个选项,persp()
和 wireframe()
,后者在 lattice 包中。
首先是一些虚拟数据:
set.seed(3)
dat <- data.frame(Dates = rep(seq(Sys.Date(), Sys.Date() + 9, by = 1),
each = 24),
Times = rep(0:23, times = 10),
Value = rep(c(0:12,11:1), times = 10) + rnorm(240))
persp()
需要作为 x
和 y
网格位置的数据以及观察矩阵 z
.
new.dates <- with(dat, sort(unique(Dates)))
new.times <- with(dat, sort(unique(Times)))
new.values <- with(dat, matrix(Value, nrow = 10, ncol = 24, byrow = TRUE))
并且可以使用以下方法绘制:
persp(new.dates, new.times, new.values, ticktype = "detailed", r = 10,
theta = 35, scale = FALSE)
可以使用 col
参数对刻面进行着色。您可能比研究您链接到的页面上的 chartSeries3d0()
代码做得更糟。大多数代码只是绘制适当的轴,因为 persp()
和 wireframe()
都无法轻松处理 Date
对象。
至于wireframe()
,我们
require(lattice)
wireframe(Value ~ as.numeric(Dates) + Times, data = dat, drape = TRUE)
您需要做一些工作来整理轴标签,因为 wireframe()
不适用于 "Date"
类的对象时刻(因此转换为数字)。
关于r - 从 R 中的时间序列数据制作 3D 表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6432996/