[英]R Layers of Color in a Multivariate Plot
How do we make different layers of colours to better see a Multivariate Function in R? 我们如何制作不同的颜色层以更好地查看R中的多元函数? I would like to check whether the function is Quasi-Concave and Quasi-Convex, but these things are hard to see in 1 one-colour plot... 我想检查函数是否为准凹和准凸,但是这些东西很难在1种单色图中看到。
# Define Sequences for Multivariate Function
xf3x1 <- seq(-100, 100, length=500)
xf3x2 <- seq(-100, 100, length=500)
# Outer Calculates the Cartesian Product
z <- outer(xf3x1,xf3x2,function(xf3x1,xf3x2) xf3x1*xf3x2)
persp(xf3x1,xf3x2,z,col="lightgreen",theta=30,phi=20, main="Problème 3: Function 3")
First, you need to set border=NA
to turn off the borders around the surface facets. 首先,您需要设置border=NA
以关闭表面小平面周围的边界。
One way is to color based on z-value. 一种方法是根据z值进行着色。 Adapting this post : 改编这篇文章 :
nrz <- nrow(z)
ncz <- ncol(z)
color <- rev(rainbow(100))
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, 100)
persp(xf3x1,xf3x2,z,col=color[facetcol],border=NA,,theta=30,phi=20)
I'd encourage you to try surface3d(...)
in the rgl
package, which creates rotatable 3D plots. 我鼓励您尝试在rgl
程序包中使用surface3d(...)
,它会创建可旋转的3D图。 The image below is just a screen shot. 下图只是屏幕截图。
zlen <- diff(range(z)) + 1
clrs <- rev(rainbow(zlen))
col <- clrs[ z - min(z) + 1 ] # assign colors to heights for each point
open3d(scale=c(100,100,1))
surface3d(xf3x1,xf3x2,z,col=col, main="Problème 3: Function 3")
axes3d(box=TRUE)
title3d(xlab="xf3f1", ylab="xf3x2", zlab="z")
The plot3D
package is an additional option. plot3D
软件包是一个附加选项。 persp3D
colors by z-value by default: persp3D
颜色默认为z值:
library(plot3D) # For persp3D function
# Define Sequences for Multivariate Function
#### length=50 to speed up plotting ####
xf3x1 <- seq(-100, 100, length=50)
xf3x2 <- seq(-100, 100, length=50)
# Outer Calculates the Cartesian Product
z <- outer(xf3x1,xf3x2,function(xf3x1,xf3x2) xf3x1*xf3x2)
persp(xf3x1,xf3x2, z, theta=30, phi=20,
col="lightgreen",
main="persp: Black border lines overwhelm plot")
persp(xf3x1,xf3x2, z, theta=30, phi=20,
col="lightgreen",
border="black", lwd=0.2, # Or border=NA per @jlhoward
main="persp: Thinner border lines")
persp3D(xf3x1,xf3x2, z, theta=30, phi=20,
main="persp3D: No borders by default")
persp3D(xf3x1,xf3x2, z, theta=30, phi=20,
border="black", lwd=0.5,
main="persp3D with borders")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.