Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:4D绘图,x,y,z,颜色_R_Plot - Fatal编程技术网

R:4D绘图,x,y,z,颜色

R:4D绘图,x,y,z,颜色,r,plot,R,Plot,你能给我一个关于如何使用rgl在x、y和z轴上绘制3个变量和一个不同颜色的第四个变量的例子吗 谢谢看看示例(第3d点) r3d帮助页面显示如何绘制轴 x <- c(0, 10, 0, 0) y <- c(0, 0, 100, 0) z <- c(0, 0, 0, 1) i <- c(1,2,1,3,1,4) labels <- c("Origin", "X", "Y", "Z") text3d(x,y,z,labels) segments3d(x[i],y[i],z

你能给我一个关于如何使用rgl在x、y和z轴上绘制3个变量和一个不同颜色的第四个变量的例子吗


谢谢

看看
示例(第3d点)

r3d
帮助页面显示如何绘制轴

x <- c(0, 10, 0, 0)
y <- c(0, 0, 100, 0)
z <- c(0, 0, 0, 1)
i <- c(1,2,1,3,1,4)
labels <- c("Origin", "X", "Y", "Z")
text3d(x,y,z,labels)
segments3d(x[i],y[i],z[i])

x如果您谈论的是在三维空间中绘制点并为其着色,则?plot3d中有一个示例:

x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
我为获得
Z2
所做的详细工作可能并不重要,但我试图获得与您链接的图形类似的内容

如果我还没有得到你想要的,你能用一些示例数据编辑你的Q,让我们更好地了解你想要什么吗

X <- 1:10
Y <- 1:10
## Z is now a 100 row object of X,Y combinations
Z <- expand.grid(X = X, Y = Y)
## Add in Z1, which is the 3rd variable
## X,Y,Z1 define the surface, which we colour according to
## 4th variable Z2
Z <- within(Z, {
    Z1 <- 1.2 + (1.4 * X) + (-1.9 * Y)
    Z2 <- 1.2 + (1.4 * X) - (1.2 * X^2) + (1.9 * Y) + (-1.3 * Y^2)
    Z3 <- 1.2 + (1.4 * X) + (-1.9 * Y) + (-X^2) + (-Y^2)})
## show the data
head(Z)
## Set-up the rgl device
with(Z, plot3d(X, Y, Z1, type = "n"))
## Need a scale for Z2 to display as colours
## Here I choose 10 equally spaced colours from a palette
cols <- heat.colors(10)
## Break Z2 into 10 equal regions
cuts <- with(Z, cut(Z2, breaks = 10))
## Add in the surface, colouring by Z2
with(Z, surface3d(1:10,1:10, matrix(Z1, ncol = 10),
                   color = cols[cuts], back = "fill"))
with(Z, points3d(X, Y, Z1, size = 5)) ## show grid X,Y,Z1

HTH

您可以根据单独的功能组合使用
persp
和颜色。下面是一些示例代码:

## Create a simple surface  f(x,y) = -x^2 - y^2
## Colour the surface according to x^2 only
nx = 31; ny = 31
x = seq(-1, 1, length = nx)
y = seq(-1, 1, length = ny)
z = outer(x, y, function(x,y) -x^2  -y^2)
## Fourth dim
z_col = outer(x, y, function(x,y) x^2)

## Average the values at the corner of each facet
## and scale to a value in [0, 1].  We will use this
## to select a gray for colouring the facet. 
hgt = 0.25 * (z_col[-nx,-ny] + z_col[-1,-ny] + z_col[-nx,-1] + z_col[-1,-1])
hgt = (hgt - min(hgt))/ (max(hgt) - min(hgt))

##  Plot the surface with the specified facet colours.
persp(x, y, z, col = gray(1 - hgt))
persp(x, y, z, col=cm.colors(32)[floor(31*hgt+1)], theta=-35, phi=10)
这使得:

RGL

在rgl库中使用上述技术非常简单:

library(rgl)
## Generate the data using the above commands
## New window
open3d()

## clear scene:
clear3d("all")

## setup env:
bg3d(color="#887777")
light3d()

surface3d(x, y, z, color=cm.colors(32)[floor(31*hgt+1)], alpha=0.5)

像这样的。但是有一点confusing@user425895:我编辑了我的答案,试图得到类似于您链接到的图表的内容。这种努力更像你想要的吗?道歉;在评论之前,我正在整理我的代码,
Z2
是我使用的对象的旧名称。我在评论中编辑了示例,但最后一行是多余的,只是为了显示X、Y、Z1点的位置。嗨。非常感谢你的奉献精神。您的示例生成了一个平面,我正在寻找一个类似于csgillespie提供的图。这只是因为我没有为弯曲的
Z1
生成虚拟数据。原理是一样的。@user425895:您应该看到3个带标签的轴和一些点。检查是否可以使用
rgl
显示任何内容。然后检查复制和粘贴是否正确。
graster::plot3D
使用
rgl
。我在这里创建了示例数据的图像:晚了5年的注释:
cm.colors(31)[floor(31*hgt+1)]
将给出一些越界值(NA),因为
hgt
可以是1。你可能是指
cm.颜色(32)[地板(31*hgt+1)]
## Set-up the rgl device plotting Z3, a curved surface
with(Z, plot3d(X, Y, Z3, type = "n"))
with(Z, surface3d(1:10,1:10, matrix(Z3, ncol = 10),
                   color = cols[cuts], back = "fill"))
## Create a simple surface  f(x,y) = -x^2 - y^2
## Colour the surface according to x^2 only
nx = 31; ny = 31
x = seq(-1, 1, length = nx)
y = seq(-1, 1, length = ny)
z = outer(x, y, function(x,y) -x^2  -y^2)
## Fourth dim
z_col = outer(x, y, function(x,y) x^2)

## Average the values at the corner of each facet
## and scale to a value in [0, 1].  We will use this
## to select a gray for colouring the facet. 
hgt = 0.25 * (z_col[-nx,-ny] + z_col[-1,-ny] + z_col[-nx,-1] + z_col[-1,-1])
hgt = (hgt - min(hgt))/ (max(hgt) - min(hgt))

##  Plot the surface with the specified facet colours.
persp(x, y, z, col = gray(1 - hgt))
persp(x, y, z, col=cm.colors(32)[floor(31*hgt+1)], theta=-35, phi=10)
library(rgl)
## Generate the data using the above commands
## New window
open3d()

## clear scene:
clear3d("all")

## setup env:
bg3d(color="#887777")
light3d()

surface3d(x, y, z, color=cm.colors(32)[floor(31*hgt+1)], alpha=0.5)