R中时间序列的曲面图

23c0lvtd  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(79)

曲面图可以直接从R中的每日数据的时间序列创建吗?
该曲面的x轴为月份,y轴为年份,z轴为数值。

dt <- seq(as.Date('2006-01-01'),as.Date('2020-01-31'),by = 1)
temp <- rnorm(5144, mean=21, sd=5)

df <- data. Frame(dt, temp)

字符串

pexxcrt2

pexxcrt21#

我假设temptemperature的缩写。考虑到你的玩具数据,heatmap似乎是合适的

dt <- seq(as.Date('2006-01-01'),as.Date('2020-01-31'),by = 1)
temp <- rnorm(5144, mean = 21, sd = 5)
df <- data.frame(dt, temp)

library(dplyr)
library(lubridate)
library(plotly)

df |>
  mutate(month = month(dt), 
         year = year(dt)) |>
  plot_ly(x = ~ month, y = ~ year, z = ~ temp, type = 'heatmap')

字符串

曲面图需要将矩阵传递给z,请参见?add_surface

thtygnil

thtygnil2#

如果您要查找3D表面打印,则可以转换为矩阵格式并使用plotly打印

dt   <- seq(as.Date('2006-01-01'), as.Date('2020-12-31'), by = 1)
temp <- rnorm(length(dt), mean = 21, sd = 5)
df   <- data.frame(dt, temp)

library(plotly)

df$month <- as.numeric(substr(df$dt, 6, 7))
df$year <- as.numeric(substr(df$dt, 1, 4))
df <- aggregate(temp ~ year + month, data = df, mean)
m <- matrix(df$temp, ncol = 12)
dimnames(m) <- list(sort(unique(df$year)), sort(unique(df$month)))

plot_ly(z = ~m) |> add_surface() |> 
  layout(scene = list(yaxis = list(title = "Year", tickvals = 0:14,
                                   ticktext = 2006:2020),
                      xaxis = list(title = "Month", tickvals = 0:11,
                                   ticktext = month.abb),
                      zaxis = list(title = "Temperature")))

字符串

相关问题