在 R 时间内为系列数据绘制 3D 曲面图曲面、时间内、系列、数据

2023-09-06 07:53:59 作者:你不用多猜,我谁都不爱

有没有办法在 R 中使用 plotly 绘制时间序列的 3D 表面.我已经尝试了几种方法,但无法获得有意义的表示.

Is there any way of plotting 3D surface in R using plotly for time series. I have tried several ways of doing it and cannot get a meaningful representation.

这是我的数据的样子:

                0.25       0.5      0.75         1         2          3          4          5          7         10
1991-01-02 0.1099018 0.1075803 0.1060026 0.1048713 0.1026496 0.10156800 0.10040423 0.09918249 0.09782469 0.09941434
1991-01-03 0.1107301 0.1069789 0.1049246 0.1035827 0.1013430 0.10060837 0.09977042 0.09876087 0.09755911 0.09913463
1991-01-04 0.1096955 0.1057843 0.1037788 0.1025263 0.1005516 0.09989326 0.09913758 0.09828665 0.09744314 0.09923485
1991-01-07 0.1081831 0.1055844 0.1041088 0.1031551 0.1016053 0.10098120 0.10020332 0.09935368 0.09857824 0.10051402
1991-01-08 0.1070853 0.1046270 0.1035221 0.1028750 0.1018182 0.10118515 0.10035681 0.09950923 0.09881024 0.10080843
1991-01-09 0.1067455 0.1040249 0.1028210 0.1021501 0.1013992 0.10130785 0.10093757 0.10034346 0.09963237 0.10130524
                  15         20        25        30
1991-01-02 0.1013165 0.09823009 0.1002991 0.1150025
1991-01-03 0.1007016 0.09737618 0.0996668 0.1147670
1991-01-04 0.1015744 0.09906617 0.1009025 0.1154665
1991-01-07 0.1032524 0.10086460 0.1024462 0.1175229
1991-01-08 0.1036377 0.10126557 0.1027223 0.1177263
1991-01-09 0.1043398 0.10207753 0.1032627 0.1183408

这是迄今为止我能得到的最接近的:

This is the closest I can get so far:

plot_ly(z = ~as.matrix(mdf)) %>%
  add_surface()

如您所见,它完全忽略了所需的 xy 轴.任何帮助将不胜感激!

As you can see, it completely ignores desired x and y axes. Any help would be highly appreciated!

推荐答案

如果您希望绘图考虑行名和列名,则需要将它们作为参数传递(见下文).

If you want the plot to take the row- and column names into account you'll need to pass them as arguments (see below).

PS:下次请使用dput(head(mdf))分享你的数据.

PS: Next time please use dput(head(mdf)) to share your data.

library(plotly)

mdf <- structure(list(`0.25` = c(0.1099018, 0.1107301, 0.1096955, 0.1081831,
0.1070853, 0.1067455), `0.5` = c(0.1075803, 0.1069789, 0.1057843, 0.1055844,
0.104627, 0.1040249), `0.75` = c(0.1060026, 0.1049246, 0.1037788, 0.1041088,
0.1035221, 0.102821), `1` = c(0.1048713, 0.1035827, 0.1025263, 0.1031551,
0.102875, 0.1021501), `2` = c(0.1026496, 0.101343, 0.1005516, 0.1016053,
0.1018182, 0.1013992), `3` = c(0.101568, 0.10060837, 0.09989326, 0.1009812,
0.10118515, 0.10130785), `4` = c(0.10040423, 0.09977042, 0.09913758,
0.10020332, 0.10035681, 0.10093757), `5` = c(0.09918249, 0.09876087,
0.09828665, 0.09935368, 0.09950923, 0.10034346), `7` = c(0.09782469,
0.09755911, 0.09744314, 0.09857824, 0.09881024, 0.09963237), `10` =
c(0.09941434, 0.09913463, 0.09923485, 0.10051402, 0.10080843, 0.10130524 ),
`15` = c(0.1013165, 0.1007016, 0.1015744, 0.1032524, 0.1036377, 0.1043398),
`20` = c(0.09823009, 0.09737618, 0.09906617, 0.1008646, 0.10126557,
0.10207753), `25` = c(0.1002991, 0.0996668, 0.1009025, 0.1024462, 0.1027223,
0.1032627), `30` = c(0.1150025, 0.114767, 0.1154665, 0.1175229, 0.1177263,
0.1183408)), class = "data.frame", row.names = c("1991-01-02", "1991-01-03",
"1991-01-04", "1991-01-07", "1991-01-08", "1991-01-09" ))

plot_ly(
  z = ~ as.matrix(mdf),
  x = ~ as.Date(rownames(mdf)),
  y = ~ as.numeric(colnames(mdf)),
  type = "surface",
  colorbar = list(title = "My surface plot")
) %>% layout(scene = list(
  xaxis = list(title = "X-Axis"),
  yaxis = list(title = "Y-Axis"),
  zaxis = list(title = "Z-Axis")
))