如何在 R 中绘制三维图 [英] How to Draw a three-dimensional graph in R

查看:77
本文介绍了如何在 R 中绘制三维图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f(x,y)= (1/25)*(20-x)/x   10<x<20, x/2 <y <x
        0                   o.t

我必须通过这个表达式来创建这个图像.

I have to create this image through this expression.

但是

x <- seq(10, 20, length=20)
y <- seq(10, 20, length=20)
f <- function(x,y){(1/25)*(20-x)/5}
z <- outer(x,y,f)
persp(x,y,z,theta=30,phi=30, expand=0.5,col=rainbow(19), border=NA)

怎么了?

推荐答案

@SRhm 的回答可能是最好的选择,但如果你想生活在最前沿,你可以使用开发版摆脱锯齿状的对角线边缘rgl (来自 R-forge),至少版本 0.100.8.

@SRhm's answer is probably the best choice, but if you want to live on the bleeding edge, you can get rid of the jagged diagonal edge using a development version of rgl (from R-forge), at least version 0.100.8.

此版本使用 tripack 包支持带边界的三角剖分.因此,您在 x-y 范围内设置了一个值网格,然后使用方程定义区域的边界,您将获得平滑的边缘.例如:

This version supports triangulations with boundaries using the tripack package. So you set up a grid of values over the x-y range, then define the boundaries of the region using the equations, and you get smooth edges. For example:

library(tripack)
library(rgl)
g <- expand.grid(x=10:20, y=5:20)
keep <- with(g, 10 < x & x < 20 & x/2 < y & y < x)
g2 <- g[keep,]
tri <- tri.mesh(g2)

# Set up boundary constraints
cx <- c(10:20, 20: 10)
cy <- c(seq(5, 10, len=11), 20:10)
tri2 <- add.constraint(tri, cx, cy, reverse = TRUE)

# This isn't necessary, but shows where the formula will be evaluated
plot(tri2)

用更多的点填充一些左右边缘可能会更好,以避免那些大三角形,但现在跳过它.

It might be better to fill in some of the left and right edges with more points to avoid those big triangles, but skip that for now.

z <- with(tri2, (1/25)*(20-x)/x)
# Now plot it, using the map2color function @SRhm found:
#source: https://stackoverflow.com/questions/50079316/plot3d-how-to-change-z-axis-surface-color-to-heat-map-color
map2color <- function(x, pal, limits = range(x,na.rm=T)){
  pal[findInterval(x, seq(limits[1], limits[2], length.out = length(pal) + 1), 
               all.inside=TRUE)]
}
persp3d(tri2, z, col = map2color(z, rainbow(100)))

旋转后,您会看到此视图:

After rotation, you get this view:

这篇关于如何在 R 中绘制三维图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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