% pivot_wider(c(time,value,order), names_from = time, -6ren">
gpt4 book ai didi

r - 如何在 R 中使用 plotly 绘制 3d 曲面图?

转载 作者:行者123 更新时间:2023-12-04 15:06:59 25 4
gpt4 key购买 nike

我有以下数据集:

group<-c(rep("X1",5),rep("X3",5))
set.seed(1)
value<-c(seq(0.2, 1, .2),seq(10, 30, 5))
time<-c("2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 02:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00",
"2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00","2018-07-01 05:00:00")
order<-c(1,2,3,4,5,1,2,4,5,6)
country<-c("HU","ZA","XX","ZZ","RO","HU","ZA","XX","ZZ","RO")
dat <-data.frame(time,country,group,value,order)

我想绘制一个 3d 图,其中 x=order, Y=value, z=hour(time)。我在这里读到 z 应该有维度 [x,y]。 3d Surface Plot in R with plotly4个应该得到这样的东西:

enter image description here

我该如何绘制它,因为我不明白如何为具有该维度的 Z 创建矩阵?

最佳答案

您的数据似乎不适合 3D。不能有两个具有相同 x 轴和 y 轴的 z 值。如果你过滤掉其中一个组,你可以在技术上让它发挥作用,但我认为其他一些图表会更好。下面是一个示例,说明如何让它与一个小组一起工作,以及一个可能效果更好的 plotly 示例。

library(plotly)

group<-c(rep("X1",5),rep("X3",5))
set.seed(1)
value<-c(seq(0.2, 1, .2),seq(10, 30, 5))
time<-c("2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 02:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00",
"2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00","2018-07-01 05:00:00")
order<-c(1,2,3,4,5,1,2,4,5,6)
country<-c("HU","ZA","XX","ZZ","RO","HU","ZA","XX","ZZ","RO")
dat <-data.frame(time,country,group,value,order)

library(tidyverse)
m = dat %>%
filter(group == "X1") %>%
pivot_wider(c(time,value,order), names_from = time,
values_from = value) %>%
select(-order) %>%
as.matrix
m[] = case_when(is.na(m) ~ 0, TRUE ~ m)
plot_ly(z = ~m) %>%
add_surface()

plot_ly(data = dat, x = ~time, y = ~value, color = ~group)

这是一个更适合的数据示例

m = matrix(rnorm(25),nrow = 5, ncol = 5)

plot_ly(z = ~m) %>%
add_surface()

关于r - 如何在 R 中使用 plotly 绘制 3d 曲面图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65942132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com