scatterplot3d:一个特立独行的R包
标签:
scatterplot3d杂谈 |
分类: R语言 |
这个函数的参数多到让人眼花缭乱,下面是几个例子。
首先,用scatterplot3d()画一个螺旋线,
> z <- seq(-10, 10, 0.01);x <- cos(z); y <- sin(z)
> scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis="blue",col.grid="lightblue", +main="scatterplot3d - 1", pch=20)
http://f.dataguru.cn/farattach/portal/201204/05/211352kmquctkz9eemrdqk.png
再画半椭圆,
>
>
>
>
+
http://f.dataguru.cn/farattach/portal/201204/05/211420krrmvzdsl9qsgxxj.png
将两者结合,很像龙盘柱,有点酷,
> x <- c(rep(1, 50) %*% t(cos(temp)))
> y <- c(cos(temp) %*% t(sin(temp)))
> z <- 10 * c(sin(temp) %*% t(sin(temp)))
> color <- rep("green", length(x))
> temp <- seq(-10, 10, 0.01)
> x <- c(x, cos(temp))
> y <- c(y, sin(temp))
> z <- c(z, temp)
> color <- c(color, rep("red", length(temp)))
> scatterplot3d(x, y, z, color, pch=20, zlim=c(-2, 10),
+ main="scatterplot3d - 3")
http://f.dataguru.cn/farattach/portal/201204/05/211438bpl1nkc6ctcosz0i.png
将两者结合,很像龙盘柱,有点酷,
> x <- c(rep(1, 50) %*% t(cos(temp)))
> y <- c(cos(temp) %*% t(sin(temp)))
> z <- 10 * c(sin(temp) %*% t(sin(temp)))
> color <- rep("green", length(x))
> temp <- seq(-10, 10, 0.01)
> x <- c(x, cos(temp))
> y <- c(y, sin(temp))
> z <- c(z, temp)
> color <- c(color, rep("red", length(temp)))
> scatterplot3d(x, y, z, color, pch=20, zlim=c(-2, 10),
+ main="scatterplot3d - 3")
http://f.dataguru.cn/farattach/portal/201204/05/211458rk5ykymlhacvwmcf.png
上面的可以说都不是 很实用,下面看一下二维回归平面图,
> data(trees)
> s3d <- scatterplot3d(trees, type="h", highlight.3d=TRUE,
+ angle=55, scale.y=0.7, pch=16, main="scatterplot3d - 5")
> s3d$points3d(seq(10,20,2), seq(85,60,-5), seq(60,10,-10),
+ col="blue", type="h", pch=16)
# 画回归平面
> attach(trees)
The following object(s) are masked from 'trees (position 3)':
Girth, Height, Volume
> my.lm <- lm(Volume ~ Girth + Height)
> s3d$plane3d(my.lm, lty.box = "solid")
http://f.dataguru.cn/farattach/portal/201204/05/211518ov9446tv9w4oovw9.png
我看见有很多帖子索要二维正态分布的示意图,这里画一个,
> library(mvtnorm)
> x1 <- seq(-10, 10, length = 51)
> x2 <- seq(-10, 10, length = 51)
> dens <-
matrix(dmvnorm(expand.grid(x1,x2),sigma=rbind(c(3,2),c(2,3))),ncol=length(x1))
>
s3d<-scatterplot3d(x1,x2,seq(min(dens),max(dens),length=length(x1)),
+ type="n",grid=FALSE,angle = 70,
+ zlab=expression_r(f(x[1],x[2])),
+ xlab=expression_r(x[1]),ylab=expression_r(x[2]),
+ main="Bivariate normal distribution")
> text(s3d$xyz.convert(-1,10,0.07),
+labels=expression_r(f(x)==frac(1,sqrt((2*pi)^n*phantom(".")*det(Sigma[X])))*phantom(".")*exp(bgroup("(",-scriptstyle(frac(1,2)*phantom("."))*(x-mu)^T*Sigma[X]^-1*(x-mu),")"))))
> for(i in length(x1):1)
+ s3d$points3d(rep(x1[i], length(x2)), x2, dens[i,], type =
"l")
> for(i in length(x2):1)
+ s3d$points3d(x1, rep(x2[i], length(x1)), dens[,i], type =
"l")
http://f.dataguru.cn/farattach/portal/201204/05/211547pz9b0pwvfjbzb1ob.png
> cubedraw <- function(res3d, min = 0, max = 255, cex = 2, text. = FALSE)
+ {
+ cube01 <- rbind(c(0,0,1), 0, c(1,0,0), c(1,1,0), 1, c(0,1,1), # < 6 outer
+ c(1,0,1), c(0,1,0))
+ cub <- min + (max-min)* cube01
+ res3d$points3d(cub[c(1:6,1,7,3,7,5) ,], cex = cex, type = 'b', lty = 1)
+ res3d$points3d(cub[c(2,8,4,8,6), ], cex = cex, type = 'b', lty = 3)
+ if(text.)
+ text(res3d$xyz.convert(cub), labels=1:nrow(cub), col='tomato', cex=2)
+ }
> cc <- colors()
> crgb <- t(col2rgb(cc))
> par(xpd = TRUE)
> rr <- scatterplot3d(crgb, color = cc, box = FALSE, angle = 24,
+ xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))
> cubedraw(rr)
> rbc <- rainbow(201)
> Rrb <- t(col2rgb(rbc))
> rR <- scatterplot3d(Rrb, color = rbc, box = FALSE, angle = 24,
+ xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))
> cubedraw(rR)
> rR$points3d(Rrb, col = rbc, pch = 16)
http://f.dataguru.cn/farattach/portal/201204/05/211609zk1c8ittirijuffz.png

加载中…