[英]R Layers of Color in a Multivariate Plot
我們如何制作不同的顏色層以更好地查看R中的多元函數? 我想檢查函數是否為准凹和准凸,但是這些東西很難在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")
首先,您需要設置border=NA
以關閉表面小平面周圍的邊界。
一種方法是根據z值進行着色。 改編這篇文章 :
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)
我鼓勵您嘗試在rgl
程序包中使用surface3d(...)
,它會創建可旋轉的3D圖。 下圖只是屏幕截圖。
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")
plot3D
軟件包是一個附加選項。 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.