【问题标题】:R: Evaluating and Plotting Functions Over a GridR:在网格上评估和绘制函数
【发布时间】:2021-10-01 01:42:41
【问题描述】:

我正在使用 R 编程语言。我有以下功能:

#function

my_function_b <- function(input_1, input_2, input_3, input_4) {

final_value = sin(input_1) + cos(input_2) + input_3 + input_4
 
}

问题

  • 对于“my_function_b”,我正在尝试为“input_1”、“input_2”、“input_3”和“input_4”的不同值评估“final_value”。例如。 input_1, input_2, input_3, input_4 从 1 到 100,增量为 0.1。

  • 然后,我想用“input_1”、“input_2”和“input_3”制作一个 3 维图。

  • 接下来,我想在这个图上拟合一个 3 维曲面

  • 最后,我想根据“final_input”的值“着色”这个 3 维表面

到目前为止我尝试了什么:

我想出了如何为第二个函数制作一个“网格框架”,然后使用这个“网格框架”评估“final_value”,例如

#create grid and evaluate function
input_1 <- seq(0,100,0.1)
input_2 <- seq(0,100,0.1)
input_3 <- seq(0,100,0.1)
input_4 <- seq(0,100,0.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

但我不确定这是否是解决这个问题的最佳方法。现在,当我尝试绘制结果时,这会产生问题,例如

#make a 3d plot for two of the inputs and the output, and fit surface over the plot

persp(my_grid$input_1, my_grid$input_2, my_grid$final_value)

Error in persp.default(my_grid$input_1, my_grid$input_2, my_grid$final_value) : 
  invalid 'z' argument

备选方案 #2:不起作用

library(plotly)

a = my_grid[,c(1,2,5)]
fig <- plot_ly(a = ~as.matrix(a))
fig <- fig %>% add_surface()

Error: Must supply `z` attribute

备选方案 #3:不起作用 - 创建一个空图

plot_ly() %>% 
    add_trace(data = my_grid,  x=my_grid$input_1, y=my_grid$input_2, z=my_grid$final_value, type="mesh3d" ) 

问题:有人可以告诉我怎么做吗?这可以使用“lattice”或“rsm”库来完成吗?或者可以按照我建议的方式完成吗?

谢谢

【问题讨论】:

    标签: r function plot plotly data-visualization


    【解决方案1】:

    如果你想让你的情节方法继续下去,你需要将你的 z 配置为一个矩阵,即一个具有定义行和列维度的对象!

    Plotly 不需要数据框。您可以安全地直接提供变量(对象)。

    library(plotly)
    
    x <- my_grid$input_1
    y <- my_grid$input_2
    z <- matrix(my_grid$final_value, nrow = length(x), ncol = length(y)) # proper matrix & dimensions
    
    plot_ly(x = x, y = y, z = z) %>% add_surface()
    

    瞧:

    根据评论中的问题修改答案 - 如何设置色阶

    这在文档中有点隐藏。原则上,您可以使用您喜欢的渐变的端点/颜色定义自己的色阶,并确保根据您的表面数据对其进行缩放。表面数据必须再次是定义明确的矩阵,即具有 nrow/ncol 维度check this SO post

    通常,为区间 c(0,1) 定义色阶。 plotly documentation 谈到最低 (0) 和最高 (1) 值的映射。色阶的第二个元素是光谱末端颜色的定义!作为替代方案,您可以使用一种公认的调色板。
    这个SO post 提供了一个例子:

    colorscale = list(c(0, 1), c("tan", "blue"))     
    

    因此,对于映射,您可以使用最小值和最大值。

    # --- define coordinate vectors
    x = input_1
    y = input_2
    z = matrix(input_3, nrow = length(x), ncol = length(y)) # z must be well-dimensioned matrix
    
    # --- define a scale for your colour setting
    my_colorscale = list(
        c(min(final_value),max(final_value))    # set min/max value, or use c(0,1)
      , c("tan", "blue")                        # define desired grad. colours
    )
    
    # --- plot
    plot_ly(x = x, y = y, z = z) %>% 
       add_surface(
           surfacecolor = matrix(final_value, nrow = length(x), ncol = length(y))   # your surface data points 
          ,colorscale = my_colorscale)    # scale for your surface 
    

    【讨论】:

    • 感谢您的回答!是否可以使表面的颜色代表“final_value”? IE。 “input_1”、“input_2”、“input_3”....和根据“final_value”的颜色之间的表面?非常感谢您的帮助!
    • @Noob 我修改了设置表面点和色阶的答案。继续摇摆!
    • 非常感谢您的回答!我能够想出另一种方法来做一些稍微不同的事情!
    • X % add_surface()
    • 优秀。如前所述,您不需要使用 plotly 处理数据框,但您可以。只需确保在使用时使用“波浪号”符号即可。追逐错误花费了我数小时的时间...... :)。 ....要记住的重要一点是,您必须将 z 变量格式化为具有清晰尺寸的矩阵。在这里,您的 nrow 就足够了。不错,菜鸟。
    猜你喜欢
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2013-08-31
    相关资源
    最近更新 更多