[英]Levelplot in R on irregular grid
I would like to create a 2D levelplot in R where the x and y coordinates are from an irregular grid without using interpolation. 我想在R中创建一个2D水平图,其中x和y坐标来自不规则网格,而不使用插值。 The grid is given below:
网格如下:
grid<-cbind(seq(from=0.05,to=0.5,by=0.05),seq(from=0.05,to=0.5,by=0.05))
grid<-rbind(grid,cbind(seq(from=0.0,to=0.95,by=0.05),seq (from=0.05,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.9,by=0.05),seq (from=0.1,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.85,by=0.05),seq(from=0.15,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.75,by=0.05),seq(from=0.25,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.80,by=0.05),seq(from=0.20,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.70,by=0.05),seq(from=0.30,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.65,by=0.05),seq(from=0.35,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.60,by=0.05),seq(from=0.40,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.55,by=0.05),seq(from=0.45,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.50,by=0.05),seq(from=0.50,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.40,by=0.05),seq(from=0.60,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.45,by=0.05),seq(from=0.55,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=.35,by=0.05),seq(from=0.65,to=1,by=0.05)))
grid<-rbind(grid,cbind(seq(from=0,to=0.30,by=0.05),seq(from=0.70,to=1,by=0.05)))
x=grid[,1]
y=grid[,2]
The Z-values are stored in another vector. Z值存储在另一个向量中。 I have tried to use the image-function, but without any luck.
我试图使用图像功能,但是没有任何运气。 For instance, if I try
例如,如果我尝试
image(x,y,height.vals)
where 哪里
height.vals=matrix(runif(dim(grid)[1]),nrow=dim(grid)[1],ncol=1)
I get an error message saying that x and y should be increasing. 我收到一条错误消息,说x和y应该增加。
One could use the akima function interp, but then I get interpolated data. 一个人可以使用akima函数interp,但是随后我得到了插值数据。
Looks like you have points on a 20 x 20 grid. 看起来您在20 x 20的网格上有点。 So, you can create a 20 x 20 matrix and fill it with the values from
height.vals
. 因此,您可以创建20 x 20的矩阵,并用
height.vals
的值填充它。
With a little bit of tweaking, you can turn the x
and y
values into indices of the matrix and use those indices to assign height.vals
to the appropriate places in the matrix. 稍作调整,就可以将
x
和y
值转换为矩阵的索引,并使用这些索引将height.vals
分配给height.vals
中的适当位置。
# Turn the x and y values into integers.
# R doesn't take 0 as an index, so add 1 to the x values to get rid of the 0s
inds <- cbind(x = as.integer(20*x + 1), y = as.integer(20*y))
# create the 20 x 20 matrix
m <- matrix(nrow = 20, ncol = 20)
# fill the matrix with height.vals based on the indices
m[inds] <- height.vals
Then, you can use m
as an input to functions like image
, filled.contour
, and lattice::levelplot
然后,您可以将
m
用作image
, filled.contour
和lattice::levelplot
类的函数的输入
image(m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.