创建根据 Z 轴着色的 3D 绘图 [英] Create 3D Plot Colored According to the Z-axis

查看:28
本文介绍了创建根据 Z 轴着色的 3D 绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 library(Sleuth2)

 mlr<-lm(ex1222$Buchanan2000~ex1222$Perot96*ex1222$Gore2000)


for (i in 0:3) {
           assign(paste("betaHat", i, sep=""), 
           summary(mlr)$coeff[i+1,1])
               }

x<-sort(ex1222$Perot96)
y<-sort(ex1222$Gore2000)


z1 <- outer(x, y, function(a,b) betaHat0+betaHat1*a+betaHat2*b+betaHat3*a*b)
nrz <- nrow(z)
ncz <- ncol(z)

# Create a function interpolating colors in the range of specified colors
jet.colors <- colorRampPalette( c("blue", "red") ) 

# Generate the desired number of colors from this palette
nbcol <- 100
color <- jet.colors(nbcol)

# Compute the z-value at the facet centres
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]

# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)

persp(x, y, z1, col=color[facetcol],theta=-30, lwd=.3,xlab="Perot 96", ylab="Gore 2000", zlab="Predicted Votes for Buchanan")

你好,

我正在尝试为上面的图着色.我在想我想要更高的 'z' 颜色的深红色(或任何颜色).

I am trying to color the above plot. I was thinking I want to have higher values of 'z' colored darker shades of red (or any color really).

任何有关如何实现这一目标的帮助将不胜感激.

Any help on how to make that happen would be greatly appreciated.

另外,也可以随意建议一个不同的函数来实现这一点.

Also, feel free to suggest a different function to make this happen as well.

谢谢!

编辑....我在查看 ?persp 上的示例后放置了我的新代码.我想改变颜色,但我对新情节的可读性不是很满意

推荐答案

我稍微修改了你的代码.

I modified your code a bit.

library(Sleuth2)

使用 data 参数通常比使用通过 $ 从数据框中提取的预测变量更好:

It's generally better practice to use the data argument than to use predictor variables extracted from a data frame via $:

mlr<-lm(Buchanan2000~Perot96*Gore2000,data=ex1222)

我们可以使用 expand.grid()predict() 以干净的方式获得回归结果:

We can use expand.grid() and predict() to get the regression results in a clean way:

perot <- seq(1000,40000,by=1000)
gore <-  seq(1000,400000,by=2000)

如果你想在观察的位置评估方面,你可以使用 perot <- sort(unique(ex1222$Perot96));gore <- sort(unique(ex1222$Gore2000)) 代替.

If you want the facets evaluated at the locations of the observations, you can use perot <- sort(unique(ex1222$Perot96)); gore <- sort(unique(ex1222$Gore2000)) instead.

pframe <- with(ex1222,expand.grid(Perot96=perot,Gore2000=gore))
mlrpred <- predict(mlr,newdata=pframe)

现在将预测转换为矩阵:

Now convert the predictions to a matrix:

nrz <- length(perot)
ncz <- length(gore)
z <- matrix(mlrpred,nrow=nrz)

我选择了从浅红色(#ffcccc,红色带有相当多的蓝色/绿色)到深红色(#cc0000,带有一点红色和没有别的).

I chose to go from light red (#ffcccc, red with quite a bit of blue/green) to dark red (#cc0000, a bit of red with nothing else).

jet.colors <- colorRampPalette( c("#ffcccc", "#cc0000") ) 

您也可以使用 grep("red",colors(),value=TRUE) 来查看 R 内置的内容.

You could also use grep("red",colors(),value=TRUE) to see what reds R has built in.

# Generate the desired number of colors from this palette
nbcol <- 100
color <- jet.colors(nbcol)

# Compute the z-value at the facet centres
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)

persp(perot, gore, z,
      col=color[facetcol],theta=-30, lwd=.3,
      xlab="Perot 96", ylab="Gore 2000", zlab="Predicted Votes for Buchanan")

你说你对情节的可读性不是很满意",但这不是很具体......我会花一些时间在 ?persp 页面上看看有什么你的选择是......

You say you're "not super happy with the readability" of the plot, but that's not very specific ... I would spend a while with the ?persp page to see what some of your options are ...

另一个选择是 rgl 包:

library(rgl)
## see ?persp3d for discussion of colour handling
vertcol <- cut(z, nbcol)
persp3d(perot, gore, z,
      col=color[vertcol],smooth=FALSE,lit=FALSE,
      xlab="Perot 96", ylab="Gore 2000", zlab="Predicted Votes for Buchanan")

可能还值得一看 car 包中的 scatter3d(SO 上还有其他帖子描述了如何调整其某些图形属性).

It might also be worth taking a look at scatter3d from the car package (there are other posts on SO describing how to tweak some of its graphical properties).

library(car)
scatter3d(Buchanan2000~Perot96*Gore2000,data=ex1222)

这篇关于创建根据 Z 轴着色的 3D 绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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