【发布时间】:2021-10-01 10:47:17
【问题描述】:
我正在使用 R 编程语言。我正在尝试在此处复制此 Stack Overflow 帖子中提供的答案:Color surface by variable with plotly in R
假设我有以下“数据框”(“my_grid”):
library(plotly)
library(dplyr)
#create grid and evaluate function
input_1 <- seq(0,100,1)
input_2 <- seq(0,100,1)
input_3 <- seq(0,100,1)
input_4 <- seq(0,100,1)
my_grid <- data.frame(input_1, input_2, input_3, input_4)
my_grid$final_value = sin(input_1) + cos(input_2) + input_3 + input_4
我们可以看到这个数据框的样子:
head(my_grid)
input_1 input_2 input_3 input_4 final_value
1 0 0 0 0 1.000000
2 1 1 1 1 3.381773
3 2 2 2 2 4.493151
4 3 3 3 3 5.151128
5 4 4 4 4 6.589554
6 5 5 5 5 9.324738
问题:我想用变量“input_1”、“input_2”、“input_3”制作一个 3D 曲面图 - 然后根据“final_value”为曲面着色"
plot_ly() %>%
add_trace(data = my_grid, x=my_grid$input_1, y=my_grid$input_2, z=my_grid$input_3, type="mesh3d" )
%>% add_surface(surfacecolor = my_grid$final_value,
cauto=F,
cmax=max(my_grid$final_value),
cmin=min(my_grid$final_value)
)
但这会返回几个错误,例如:
Error: unexpected SPECIAL in "%>%"Error: unexpected ',' in " cauto=F,"
我尝试了不同的方法来调试这段代码,但我似乎无法弄清楚。有人可以告诉我如何解决这些错误吗?
【问题讨论】:
-
感谢您的回复...我对数据框比较熟悉,对矩阵不太熟悉...仍在尝试弄清楚。感谢您的帮助!
-
你能查一下here
标签: r plot 3d plotly data-visualization