R:4D图,x,y,z,颜色 [英] R: 4D plot, x, y, z, colours

查看:90
本文介绍了R:4D图,x,y,z,颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否举一个例子,说明如何使用rgl在x,y和z轴上绘制3个变量以及第四个颜色不同的变量?

Could you give me an example on how to use rgl to plot 3 variables at the axes x, y and z and a fourth one with different colours?

谢谢

推荐答案

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

You use a combination of persp and colour according to a separate function. Here's some example code:

## 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库一起使用非常简单:

It's fairly straightforward to use the above technique with the rgl library:

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)

这篇关于R:4D图,x,y,z,颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆