如何创建r中像数据的数据结构(火山)

问题描述:

我有如下因素格式的一些数据:如何创建r中像数据的数据结构(火山)

y <- c(2637732, 2622262, 2637466, 2630985, 2620929, 2630888, 2625944, 2650034, 2645318, 2636731, 2629001, 2650776, 2648531, 2633905, 2654874, 2637571, 2650994, 2641130, 2652417, 2654005) 
x <- c(756994.5, 760190.9, 760898.7, 761690.2, 763064.1, 763089.3, 765942.4, 767058.1, 768265.0, 768471.8, 771393.8, 771394.1, 775332.6, 778324.8, 780480.9, 780961.0,781001.5, 783904.7, 786200.6, 788007.5) 
z <- c(0.008849558,0.260162602,0.115044248,0.109243697,0.066666667,0.000000000,0.022556391,0.157894737,0.045045045,0.378151261,0.028776978,0.128571429,0.064220183,0.148760331,0.514851485,0.173913043,0.019417476,0.037383178,0.041237113,0.150537634) 

这里是我的代码进行插值数据

df <- data.frame(x=x,y=y,z=z); 
gridint <- 500; 

xmin <- signif(min(df$x),4) - 1000; 
xmax <- signif(max(df$x),4) + 1000; 
ymin <- signif(min(df$y),5) - 1000; 
ymax <- signif(max(df$y),5) + 1000; 
yo <- seq(ymin, ymax, length=gridint); 
xo <- seq(xmin, xmax, length=gridint); 


library(akima); 
fld<- with(df, interp(x = x, y = y, z = z, linear = FALSE, extrap = TRUE, xo = xo, yo= yo)); 
fld2 <- as.data.frame(interp2xyz(fld)); 

我想创建一个像data(volcano)这样的结构来运行下面的脚本:

library(rgl); 
data(volcano) 
dim(volcano) 

peak.height <- volcano; 
ppm.index <- (1:nrow(volcano)); 
sample.index <- (1:ncol(volcano)); 

zlim <- range(peak.height) 
zlen <- zlim[2] - zlim[1] + 1 
colorlut <- terrain.colors(zlen) # height color lookup table 
col <- colorlut[(peak.height-zlim[1]+1)] # assign colors to heights for each point 
open3d() 

ppm.index1 <- ppm.index*zlim[2]/max(ppm.index); 
sample.index1 <- sample.index*zlim[2]/max(sample.index) 

title.name <- paste("volcano plot3d", sep = ""); 
surface3d(ppm.index1, sample.index1, peak.height, color=col, back="lines", main = title.name); 
grid3d(c("x", "y+", "z"), n =20) 

sample.name <- paste("col.", 1:ncol(volcano), sep=""); 
sample.label <- as.integer(seq(1, length(sample.name), length = 5)); 

axis3d('y+',at = sample.index1[sample.label], sample.name[sample.label], cex = 0.3); 
axis3d('y',at = sample.index1[sample.label], sample.name[sample.label], cex = 0.3) 
axis3d('z',pos=c(0, 0, NA)) 

ppm.label <- as.integer(seq(1, length(ppm.index), length = 10)); 
axes3d('x', at=c(ppm.index1[ppm.label], 0, 0), abs(round(ppm.index[ppm.label], 2)), cex = 0.3); 

title3d(main = title.name, sub = "test", xlab = "ppm", ylab = "samples", zlab = "peak") 
rgl.bringtotop(); 

任何人都可以帮助我吗?一些忠告?

在此先感谢

+1

您似乎没有足够的积分来支持3d表面。这些数据是什么? –

+0

数据是潜在模型(交互指数)的结果。我使用此代码来插入基础数据: df

+0

我编辑了我的问题我用来插入这些数据的代码(x,y,z) –

首先,让我们用X,Y做到这一点,你开始与z值:

str(fld) 
List of 3 
$ x: num [1:50] 756000 756673 757347 758020 758694 ... 
$ y: num [1:50] 2619900 2620635 2621369 2622104 2622839 ... 
$ z: num [1:50, 1:50] 0.255 0.256 0.257 0.258 0.259 ... 

,可以与底座的图形绘制功能persp

png(); with(fld, persp(x,y,z)) ; dev.off() 

enter image description here

现在建立一个合适的rgl阴谋,如果....我可以。事实证明,您需要将rgl的坐标范围缩放到[0-1],使其具有正确的宽高比以查看任何内容。 (也可以用aspect3d(小提琴),但我已经有一个

open3d() 
with(fld, surface3d((x -min(x))/(max(x) -min(x)), 
        (y -min(y))/(max(y) -min(y)), 
        (z -min(z))/(max(z) -min(z)))) 
rgl.snapshot("test.png") 

enter image description here

跌跌撞撞您可以使用deldir包基于原始数据显示的表面。帮助页面读的是?persp3d.deldir

library(rgl) 
open3d() 
plot3d(x, y, z) # Establish the axes, set labels etc. ... 

library(deldir) 
dxyz <- deldir(x, y, z = z, suppressMsge = TRUE) 
col <- cm.colors(20)[1 + round(19*(z - min(z))/diff(range(z)))] 
persp3d(dxyz, col = col, add = TRUE) 

产生这样的结果:

deldir result

如果您不想显示积分,则可以在plot3d呼叫中使用type = 'n',或者完全跳过它并将add = TRUE参数丢弃到persp3d。后者使得设置宽高比和标题有点困难。