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

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

问题描述

 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).

在如何使任何帮助,这种情况发生将大大AP preciated。

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.

感谢您!

修改....我把我的新code在看吗?persp的例子之后。我想改变颜色,我不是超级满意的新情节的可读性

edit....I put my new code after looking at the example on ?persp. I'd like to change the color and I'm not super happy with the readability of the new plot

推荐答案

我修改codeA位。

library(Sleuth2)

这是通常更好的做法是使用数据参数,而不是通过 $

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)

如果你想在观测的地点进行评估的方面,你可以使用珀&LT; - 排序(独特的(ex1222 $ Perot96));戈尔LT; - 排序(独特的(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)

现在转换predictions一个矩阵:

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(红,色(),值= 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")

这也可能是值得考虑看看 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)

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

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