R 中的 3d 绘图 - 补丁 [英] 3d plot in R - Patch
问题描述
我在数据框中有以下数据:
**x** in (0,1)**y** 在 [0,1]**z** 在 [0,1]
例如:
X,Y,Z0.1、0.2、0.560.1、0.3、0.57...
我想在这种类型的图表上绘制它们:
我尝试过 R,但我得到的只是一个 不那么花哨的 3d散点图.我还阅读了关于 格子 3d 线框,但我无法理解它.
我应该怎么做才能在 R 中获得类似 Matlab 的线框?涉及哪些数据转换?
这是文档中的示例代码:
x <- seq(-pi, pi, len = 20)y <- seq(-pi, pi, len = 20)g <- expand.grid(x = x, y = y)g$z <- sin(sqrt(g$x^2 + g$y^2))线框(z ~ x * y,g,悬垂=真,方面 = c(3,1), 颜色键 = TRUE)
我觉得不是特别清楚.
EDIT:persp3d
函数工作正常,我能够生成一种颜色的 3d 绘图.如何设置相对于 z 值的色阶?
感谢您的任何提示,穆隆
使用 outer
创建 z 值,然后使用 persp
绘制:
z <- 外层(x,y, function(x,y) sin(sqrt(x^2+y^2)))persp(x,y,z)
有用于着色和设置视角的选项,请参阅?persp
.参见第四个 Matlab 样式着色示例.</p>
对于交互式绘图,请考虑使用 rgl
包中的 persp3d
:
require(rgl)persp3d(x,y,z,col="blue")
编辑
要添加颜色,与persp
中的方法略有不同,因为颜色与顶点有关,而不是与刻面的中心有关,但它更容易.
jet.colors <- colorRampPalette( c("blue", "green") )朋友 <- jet.colors(100)col.ind <- cut(z,100) # 每个点的颜色索引persp3d(x,y,z,col=pal[col.ind])
帮助文件建议添加参数smooth=FALSE
,但这取决于个人喜好.
I have the following data in a data frame:
**x** in (0,1)
**y** in [0,1]
**z** in [0,1]
For example:
X,Y,Z
0.1, 0.2, 0.56
0.1, 0.3, 0.57
...
I'd like to plot them on this type of chart:
I tried on R, but all I could get was a not-so-fancy 3d scatterplot. I also read about the lattice 3d wireframe, but I couldn't get my head around it.
What am I supposed to do to get a Matlab like wireframe in R? What data transforms are involved?
This is the sample code from the documentation:
x <- seq(-pi, pi, len = 20)
y <- seq(-pi, pi, len = 20)
g <- expand.grid(x = x, y = y)
g$z <- sin(sqrt(g$x^2 + g$y^2))
wireframe(z ~ x * y, g, drape = TRUE,
aspect = c(3,1), colorkey = TRUE)
I don't find it particularly clear.
EDIT: the persp3d
function works fine, and I was able to generate a 3d plot with one colour. How can I set a colour scale relative to the z value?
Thanks for any hints, Mulone
Use outer
to create the z values and then use persp
to plot:
z <- outer(x,y, function(x,y) sin(sqrt(x^2+y^2)))
persp(x,y,z)
There are options for colouring and setting the viewing angle, see ?persp
. See the fourth example for Matlab style colouring.
For an interactive plot, consider using persp3d
in the rgl
package:
require(rgl)
persp3d(x,y,z,col="blue")
Edit
To add colour, there is a slight difference from the method in persp
, since the colour relates to the vertex rather than the centre of the facet, but it makes it easier.
jet.colors <- colorRampPalette( c("blue", "green") )
pal <- jet.colors(100)
col.ind <- cut(z,100) # colour indices of each point
persp3d(x,y,z,col=pal[col.ind])
The help file recommends adding the parameter smooth=FALSE
, but that's down to personal preference.
这篇关于R 中的 3d 绘图 - 补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!