"interp"对于离散点得到heatmap/contourR
"interp" for discrete points to get heatmap/contour R
我有一个位置,我在其中从模拟生成数据,然后想绘图(热图 map/contour/3d 绘图等);但是,对于这些,需要使用 interp
等函数对数据进行插值。这是示例 dataset.
这是我试过的一段代码...
library(akima)
library(GA) # for persp3D; there exists another package for same function "fields"
data <- read.table(commandArgs()[3], header=T,sep="\t")
data <- na.omit(data)
qmax = max(data$q)
kmax = max(data$k)
x <- data$k_bike/kmax
y <- data$k_car/kmax
z <- data$q/qmax
matrix = interp(x,y,z)
persp3D(matrix ,nlevels=30, asp=1, xlim=c(0,1), ylim=c(0,1), color.palette=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
所以结果是——
现在,由于插值,有很多点有 red/orange 颜色而不是绿色左右 。例如,如果我使用 levelplot
of lattice
levelplot(z~x*y, xlim=c(0,1), ylim=c(0,1), col.regions=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
结果是--
现在,可以清楚地看到很少有数据点具有零(或几乎为零)z
值。现在,问题是,使用 levelplot,我得到了人工制品(缺少数据点的白色),我希望有更好的插值。还有其他功能可以执行此操作吗?
我也试过等高线图如下:
scale <- (qmax+10) / qmax * c(0.000, 0.01, 0.05, 0.10, 0.25, 0.5, 0.75, 1.0)
filled.contour(matrix, nlevels=30, asp=1, xlim=c(0,1), ylim=c(0,1), levels=scale,color.palette=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
结果又是(有点错误的颜色指示)。
In short -- I would like to have contour plot or 3d plot but with a
clear (or correct) color indication of zero (about zero) z
value data
points similar to level plot.
我用 deldir
和 rgl
包解决了你的问题(它们允许绘制由不规则点集合定义的表面)。
library(deldir); library(rgl)
# Below two lines require time depending on the machine power, be careful
dxyz <- deldir(x, y, z = z) # do Delaunay triangulation
mxyz <- as.mesh3d(dxyz) # convert it to triangle.mesh3d.obj
bgyr <- colorRampPalette(c("blue", "green", "yellow", "red")) # colour func
# use z values for colouring
plot3d(mxyz, col=bgyr(256)[cut(mxyz$vb[3,], 256)][mxyz$it], type="shade")
light3d() # if you want vivit colours
# another approach
# you can solve it by just increasing interp()'s arguments, nx and ny.
library(akima); library(lattice); library(dplyr)
df <- interp(x,y,z, nx=150, ny=150) %>% interp2xyz() %>% data.frame()
levelplot(z ~ x * y, df, xlim=c(0,1), ylim=c(0,1),
col.regions = colorRampPalette(c("green3", "yellow", "red"), space = "rgb"))
我有一个位置,我在其中从模拟生成数据,然后想绘图(热图 map/contour/3d 绘图等);但是,对于这些,需要使用 interp
等函数对数据进行插值。这是示例 dataset.
这是我试过的一段代码...
library(akima)
library(GA) # for persp3D; there exists another package for same function "fields"
data <- read.table(commandArgs()[3], header=T,sep="\t")
data <- na.omit(data)
qmax = max(data$q)
kmax = max(data$k)
x <- data$k_bike/kmax
y <- data$k_car/kmax
z <- data$q/qmax
matrix = interp(x,y,z)
persp3D(matrix ,nlevels=30, asp=1, xlim=c(0,1), ylim=c(0,1), color.palette=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
所以结果是——
现在,由于插值,有很多点有 red/orange 颜色而不是绿色左右 。例如,如果我使用 levelplot
of lattice
levelplot(z~x*y, xlim=c(0,1), ylim=c(0,1), col.regions=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
结果是--
现在,可以清楚地看到很少有数据点具有零(或几乎为零)z
值。现在,问题是,使用 levelplot,我得到了人工制品(缺少数据点的白色),我希望有更好的插值。还有其他功能可以执行此操作吗?
我也试过等高线图如下:
scale <- (qmax+10) / qmax * c(0.000, 0.01, 0.05, 0.10, 0.25, 0.5, 0.75, 1.0)
filled.contour(matrix, nlevels=30, asp=1, xlim=c(0,1), ylim=c(0,1), levels=scale,color.palette=colorRampPalette(c("green3","yellow", "red"),space = "rgb") )
结果又是(有点错误的颜色指示)。
In short -- I would like to have contour plot or 3d plot but with a clear (or correct) color indication of zero (about zero)
z
value data points similar to level plot.
我用 deldir
和 rgl
包解决了你的问题(它们允许绘制由不规则点集合定义的表面)。
library(deldir); library(rgl)
# Below two lines require time depending on the machine power, be careful
dxyz <- deldir(x, y, z = z) # do Delaunay triangulation
mxyz <- as.mesh3d(dxyz) # convert it to triangle.mesh3d.obj
bgyr <- colorRampPalette(c("blue", "green", "yellow", "red")) # colour func
# use z values for colouring
plot3d(mxyz, col=bgyr(256)[cut(mxyz$vb[3,], 256)][mxyz$it], type="shade")
light3d() # if you want vivit colours
# another approach
# you can solve it by just increasing interp()'s arguments, nx and ny.
library(akima); library(lattice); library(dplyr)
df <- interp(x,y,z, nx=150, ny=150) %>% interp2xyz() %>% data.frame()
levelplot(z ~ x * y, df, xlim=c(0,1), ylim=c(0,1),
col.regions = colorRampPalette(c("green3", "yellow", "red"), space = "rgb"))