如何在r中创建数据(火山)之类的数据结构

时间:2017-05-06 23:22:53

标签: r rgl

我有以下格式的数据:

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 个答案:

答案 0 :(得分:1)

首先让我们用你开始的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