在不规则网格上绘制和着色数据 [英] plotting and coloring data on irregular grid

查看:186
本文介绍了在不规则网格上绘制和着色数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有(x,y,z)形式的数据,其中x和y不在常规网格上.我希望显示这些数据的2D颜色图,并将强度(例如灰度)映射到z变量.一个明显的解决方案是在常规网格上进行插值(见下文)

I have data in the form (x, y, z) where x and y are not on a regular grid. I wish to display a 2D colormap of these data, with intensity (say, grey scale) mapped to the z variable. An obvious solution is to interpolate (see below) on a regular grid,

d <- data.frame(x=runif(1e3, 0, 30), y=runif(1e3, 0, 30))
d$z = (d$x - 15)^2 + (d$y - 15)^2


library(akima)
d2 <- with(d, interp(x, y, z, xo=seq(0, 30, length = 30),
                     yo=seq(0, 30, length = 50), duplicate="mean"))

pal1 <- grey(seq(0,1,leng=500))
with(d2, image(sort(x), sort(y), z, useRaster=TRUE, col = pal1))
points(d$x, d$y, col="white", bg=grey(d$z/max(d$z)), pch=21, cex=1,lwd=0.1)

但是,这会丢失初始网格(带有实际数据的点的位置)的信息,该信息在某些位置可能非常好或非常粗糙.我更喜欢使用三角形的Delaunay拼贴,该拼贴可准确表示原始数据点的实际位置和密度.

However, this loses the information of the initial mesh (position of the points with actual data), which could be very fine or very rough at certain locations. My preference would be for a delaunay tiling with triangles, which accurately represents the actual location and density of the original data points.

理想的解决方案是

  • 在绘图功能之外计算网格化,以便可以使用ggplot2lattice或基础图形

要快.在我的实际示例中(〜1e5点),通过deldir进行镶嵌细分的计算可能真的很慢.

be fast. In my real-life example (~1e5 points), the calculation of the tesselation via deldir can be really slow.

镶嵌"是指Delaunay三角形或Voronoi图,尽管我更喜欢前者.但是,这会带来额外的复杂性,即根据原始数据点对每个三角形的颜色进行插值.

By "tesselation" I mean either Delaunay triangles or Voronoi diagrams, although my preference would be for the former. However it bring the additional complexity of interpolating the colour of each triangle based on the original data points.

推荐答案

这是基于maptools包中dirichlet的解决方案,

Here's a solution based on dirichlet from the maptools package,

d <- data.frame(x=runif(1e3, 0, 30), y=runif(1e3, 0, 30))
d$z = (d$x - 15)^2 + (d$y - 15)^2

library(spatstat) 
library(maptools)

W <- ripras(df, shape="rectangle") 
W <- owin(c(0, 30), c(0, 30)) 
X <- as.ppp(d, W=W) 
Y <- dirichlet(X) 
Z <- as(Y, "SpatialPolygons") 
plot(Z, col=grey(d$z/max(d$z)))

我仍然不确定从SpatialPolygons类提取多边形的方法.

I'm still unsure of the way to extract the polygons from this SpatialPolygons class.

如果有一种简便的方法可以为相关的delaunay镶嵌生成正确的"颜色,我想听听.

Also if there's an easy way to produce the "correct" colors for the associated delaunay tesselation I'd like to hear it.

这篇关于在不规则网格上绘制和着色数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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