在3d rstudio中绘制曲线的连续序列(例如浓度,时间)

问题描述 投票:1回答:1

我正在一台机器上运行样本,我的输出是无单位系数y与速度x的关系图。我改变了给定化合物的浓度,以发现曲线如何随浓度z而变化。是否有一个程序包可以绘制以这种方式设置的3d图形数据,即作为序列?

[我发现,当变量大小相等的向量(即每个点是一个样本)时,rstudio中3d绘图(格子,scatterplot3d,rgl等)的常用软件包似乎才起作用:

“常见示例x,y,z数据设置”

我的数据看起来更像这样,其中每个“切片”可以说在z轴上代表一个样本:

“示例系列数据”

我可以使用曲面图在Excel中相当容易地绘制它,但是那里的图很烂,丑陋和不灵活。

我想要的图表的最佳示例是:

nytimes sample 3d plot

来源:https://www.nytimes.com/interactive/2015/03/19/upshot/3d-yield-curve-economic-growth.html?ref=economy&abt=0002&abg=0

以下是我的数据示例。第一列包含x值(速度),第一列包含z值(浓度),其他所有内容均为y值(任意无单位系数)。因此,第一个单元格(0)不是数据的一部分。

structure(list(...1 = c(0, 1.17721e-08, 1.48056166666667e-08, 
1.86639666666667e-08, 2.35371333333333e-08, 2.96521333333333e-08
), ...2 = c(0.3, 0.000326036, 0.000789132, 0.00132642, 0.00196508, 
0.00272198), ...3 = c(3, 0.00038811, 0.000915256, 0.00152748, 
0.00224812, 0.00311174), ...4 = c(6, 0.000289036, 0.000692406, 
0.00115582, 0.00169984, 0.00234574), ...5 = c(15, 0.000266082, 
0.000647076, 0.00109054, 0.00161336, 0.00224112), ...6 = c(30, 
0.000361498, 0.000818448, 0.00134682, 0.00196732, 0.00271138), 
...7 = c(100, 0.000296934, 0.000714832, 0.0012111, 0.0018063, 
0.0025273)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

我将'dput()'存储为'example.data',并运行以下命令以获取此错误消息:

#Store x and y as vectors, z as matrix 
> x <- c(example.data[-1,1])
> y <- c(example.data[1,-1])
> z <- as.matrix(example.data[-1,-1], nrow = 5, ncol = 6)
> persp3d(x, z, y)
Error in rgl.surface(x = NULL, y = list(...2 = 0.3, ...3 = 3, ...4 = 6,  : 
'y' length != 'x' rows * 'z' cols

我在这里做错了什么? x和y分别具有与矩阵z

相同的行数和列数
r graph 3d
1个回答
0
投票

使用示例数据:

dat <- structure(list(...1 = c(0, 1.17721e-08, 1.48056166666667e-08, 
1.86639666666667e-08, 2.35371333333333e-08, 2.96521333333333e-08
), ...2 = c(0.3, 0.000326036, 0.000789132, 0.00132642, 0.00196508, 
0.00272198), ...3 = c(3, 0.00038811, 0.000915256, 0.00152748, 
0.00224812, 0.00311174), ...4 = c(6, 0.000289036, 0.000692406, 
0.00115582, 0.00169984, 0.00234574), ...5 = c(15, 0.000266082, 
0.000647076, 0.00109054, 0.00161336, 0.00224112), ...6 = c(30, 
0.000361498, 0.000818448, 0.00134682, 0.00196732, 0.00271138), 
...7 = c(100, 0.000296934, 0.000714832, 0.0012111, 0.0018063, 
0.0025273)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

通常最好将数据保留为matrix,并为此将xyz分开。您的初始子集将起作用,除了tibble故意使其像frame一样,而不是“正常” data.frame的单列/行子集,并且需要dat[...,drop=FALSE]以避免类转换。

dat[...,drop=FALSE]

x <- dat[-1,1][[1]] y <- as.numeric(dat[1,-1]) z <- as.matrix(dat[-1,-1]) rgl::persp3d(x,y,z)

您也可能已经转换为3d plot first,那么您在这里就不会受到matrix的保护。

tibble
© www.soinside.com 2019 - 2024. All rights reserved.