如何在 R 中创建带有缺失值的曲面图? [英] How can I create a surface plot with missing values in R?

查看:78
本文介绍了如何在 R 中创建带有缺失值的曲面图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下值的 5x5 矩阵示例:

I have an example 5x5 matrix with the following values:

dat <- matrix(seq(1,13,0.5), nrow=5, byrow=TRUE)
dat[seq(2,25,2)] <- NA

 1 | NA |  2 | NA |  3
NA |  4 | NA |  5 | NA
 6 | NA |  7 | NA |  8
NA |  9 | NA | 10 | NA
11 | NA | 12 | NA | 13

由于缺少值,我一生都无法使用例如 persp3d() 获得 3D 曲面图.有没有一种方法可以让 R 只插入值并仍然绘制它们?

I cannot for the life of me get a 3D surface plot using for example persp3d() because of the missing values. Isn't there a way that R can just interpolate the values and still plot them?

推荐答案

虽然之前可能有人问过这个问题,但我找不到一个很好的例子来说明这种情况.试试这个,它会给出一个你可以传递给 perspimage 等的结果...

While this might have been asked before, I couldn't find a neat worked example of this circumstance. Try this, which will give a result you can then pass to persp, image etc...

#install.packages("akima")
library(akima)

nas <- !is.na(dat)
interp(
  row(dat)[nas],       #row index   - 'x' values
  col(dat)[nas],       #col index   - 'y' values
  dat[nas],            #height data - 'z' values
  xo=seq(1,nrow(dat)), #'x' values for output
  yo=seq(1,ncol(dat))  #'y' values for output
)

#$x
#[1] 1 2 3 4 5
# 
#$y
#[1] 1 2 3 4 5
#
#$z
#     [,1] [,2] [,3] [,4] [,5]
#[1,]  1.0  1.5  2.0  2.5  3.0
#[2,]  3.5  4.0  4.5  5.0  5.5
#[3,]  6.0  6.5  7.0  7.5  8.0
#[4,]  8.5  9.0  9.5 10.0 10.5
#[5,] 11.0 11.5 12.0 12.5 13.0

这篇关于如何在 R 中创建带有缺失值的曲面图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆