简体   繁体   English

使用R绘制具有等高线图叠加的3D表面图

[英]Plotting a 3D surface plot with contour map overlay, using R

I have a 3-tuple data set (X,Y,Z points) that I want to plot using R. 我有一个3元组的数据集(X,Y,Z点),我想用R绘图。

I want to create a surface plot from the data, and superimpose a contour map on the surface plot, so as to create the impression of the contour map being the "shadow" or projection from the surface plot. 我想从数据中创建一个曲面图,并在曲面图上叠加一个等高线图,以便创建轮廓图的印象,即从表面图中“阴影”或投影。 The contour map is to appear below the surface plot. 等高线图将显示在曲面图下方。

My data set looks somewhat like this: 我的数据集看起来有点像这样:

Axis  |  Data Type
-------------------
X     |  Date value
Y     |  Float value
Z     |  Float value

How can I achieve this? 我怎样才能做到这一点?

Edit: 编辑:

I just saw that you pointed out one of your dimensions is a date. 我刚看到你指出你的一个尺寸是一个日期。 In that case, have a look at Jeff Ryan's chartSeries3d which is designed to chart 3-dimensional time series. 在这种情况下, 看看Jeff Ryan的chartSeries3d ,它是为了绘制三维时间序列而设计的。 Here he shows the yield curve over time: 他在这里显示了随时间变化的收益率曲线

chartSeries示例

Original Answer: 原答案:

As I understand it, you want a countour map to be the projection on the plane beneath the 3D surface plot. 据我了解,您希望计数地图成为3D曲面图下方平面上的投影。 I don't believe that there's an easy way to do this other than creating the two plots and then combining them. 我不相信除了创建两个图然后组合它们之外,还有一种简单的方法可以做到这一点。 You may find the spatial view helpful for this . 您可能会发现空间视图对此有帮助

There are two primary R packages for 3D plotting: rgl (or you can use the related misc3d package) and scatterplot3d . 针对3D绘制两个主要的R程序包: RGL (或者你可以使用相关的misc3d封装)和scatterplot3d

rgl RGL

The rgl package uses OpenGL to create interactive 3D plots ( read more on the rgl website ). rgl包使用OpenGL创建交互式3D图( 在rgl网站上阅读更多内容 )。 Here's an example using the surface3d function: 以下是使用surface3d函数的示例:

library(rgl)
data(volcano)
z <- 2 * volcano # Exaggerate the relief
x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W)
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x, y, z, color=col, alpha=0.75, back="lines")

The alpha parameter makes this surface partly transparent. alpha参数使该表面部分透明。 Now you have an interactive 3D plot of a surface and you want to create a countour map underneath. 现在,您有一个表面的交互式3D绘图,并且您想在下面创建一个计数地图。 rgl allows you add more plots to an existing image: rgl允许您向现有图像添加更多图:

colorlut <- heat.colors(zlen,alpha=1) # use different colors for the contour map
col <- colorlut[ z-zlim[1]+1 ] 
rgl.surface(x, y, matrix(1, nrow(z), ncol(z)),color=col, back="fill")

In this surface I set the heights=1 so that we have a plane underneath the other surface. 在这个表面我设置高度= 1,以便我们在另一个表面下面有一个平面。 This ends up looking like this, and can be rotated with a mouse: 最终看起来像这样,可以用鼠标旋转:

3D表面图

scatterplot3d scatterplot3d

scatterplot3d is a little more like other plotting functions in R ( read the vignette ). scatterplot3d更像是R中的其他绘图函数( 读取晕影 )。 Here's a simple example: 这是一个简单的例子:

temp <- seq(-pi, 0, length = 50)
x <- c(rep(1, 50) %*% t(cos(temp)))
y <- c(cos(temp) %*% t(sin(temp)))
z <- c(sin(temp) %*% t(sin(temp)))
scatterplot3d(x, y, z, highlight.3d=TRUE,
 col.axis="blue", col.grid="lightblue",
 main="scatterplot3d - 2", pch=20)

In this case, you will need to overlay the images. 在这种情况下,您需要覆盖图像。 The R-Wiki has a nice post on creating a tanslucent background image . R-Wiki 在创建tanslucent背景图像方面有一个很好的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM