<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>三维作图 on 格物堂</title><link>https://gewutang.cn/tags/%E4%B8%89%E7%BB%B4%E4%BD%9C%E5%9B%BE/</link><description>Recent content in 三维作图 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sun, 13 Jun 2010 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E4%B8%89%E7%BB%B4%E4%BD%9C%E5%9B%BE/index.xml" rel="self" type="application/rss+xml"/><item><title>scatterplot3d:一个特立独行的R包</title><link>https://gewutang.cn/2010/06/13/scatterplot3d/</link><pubDate>Sun, 13 Jun 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/06/13/scatterplot3d/</guid><description>&lt;p&gt;scatterplot3d是一个力图在3D空间展示多维数据的R包，在这个包里只含有一个函数scatterplot3d()，这一点上可谓是特立独行。scatterplot3d()的用法如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; scatterplot3d(x, y=NULL, z=NULL, color=par(&amp;quot;col&amp;quot;), pch=NULL,&#10; main=NULL, sub=NULL, xlim=NULL, ylim=NULL, zlim=NULL,&#10; xlab=NULL, ylab=NULL, zlab=NULL, scale.y=1, angle=40,&#10; axis=TRUE, tick.marks=TRUE, label.tick.marks=TRUE,&#10; x.ticklabs=NULL, y.ticklabs=NULL, z.ticklabs=NULL,&#10; y.margin.add=0, grid=TRUE, box=TRUE, lab=par(&amp;quot;lab&amp;quot;),&#10; lab.z=mean(lab[1:2]), type=&amp;quot;p&amp;quot;, highlight.3d=FALSE,&#10; mar=c(5,3,4,3)+0.1, col.axis=par(&amp;quot;col.axis&amp;quot;),&#10; col.grid=&amp;quot;grey&amp;quot;, col.lab=par(&amp;quot;col.lab&amp;quot;),&#10; cex.symbols=par(&amp;quot;cex&amp;quot;), cex.axis=0.8 * par(&amp;quot;cex.axis&amp;quot;),&#10; cex.lab=par(&amp;quot;cex.lab&amp;quot;), font.axis=par(&amp;quot;font.axis&amp;quot;),&#10; font.lab=par(&amp;quot;font.lab&amp;quot;), lty.axis=par(&amp;quot;lty&amp;quot;),&#10; lty.grid=par(&amp;quot;lty&amp;quot;), lty.hide=NULL, lty.hplot=par(&amp;quot;lty&amp;quot;),&#10; log=&amp;quot;&amp;quot;, ...)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;这个函数的参数多到让人眼花缭乱。下面是几个例子:&lt;/p&gt;&#10;&lt;p&gt;先用scatterplot3d()画一个螺旋线：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(scatterplot3d)&#10;z &amp;lt;- seq(-10, 10, 0.01)&#10;x &amp;lt;- cos(z)&#10;y &amp;lt;- sin(z)&#10;scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis=&amp;quot;blue&amp;quot;,col.grid=&amp;quot;lightblue&amp;quot;,main=&amp;quot;scatterplot3d - 1&amp;quot;, pch=20)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&#10;&lt;p&gt;再画半椭圆:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;temp&amp;lt;- seq(-pi, 0, length = 50)&#10;x &amp;lt;- c(rep(1, 50) %*% t(cos(temp)))&#10;y &amp;lt;- c(cos(temp) %*% t(sin(temp)))&#10;z &amp;lt;- c(sin(temp) %*% t(sin(temp)))&#10;scatterplot3d(x, y, z, highlight.3d=TRUE,col.axis=&amp;quot;blue&amp;quot;, col.grid=&amp;quot;lightblue&amp;quot;,main=&amp;quot;scatterplot3d - 2&amp;quot;, pch=20)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&#10;&lt;p&gt;将两者结合，输出结果很像龙盘柱。有点酷！&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; x &amp;lt;- c(rep(1, 50) %*% t(cos(temp)))&#10; y &amp;lt;- c(cos(temp) %*% t(sin(temp)))&#10; z &amp;lt;- 10 * c(sin(temp) %*% t(sin(temp)))&#10; color &amp;lt;- rep(&amp;quot;green&amp;quot;, length(x))&#10; temp &amp;lt;- seq(-10, 10, 0.01)&#10; x &amp;lt;- c(x, cos(temp))&#10; y &amp;lt;- c(y, sin(temp))&#10; z &amp;lt;- c(z, temp)&#10; color &amp;lt;- c(color, rep(&amp;quot;red&amp;quot;, length(temp)))&#10; scatterplot3d(x, y, z, color, pch=20, zlim=c(-2, 10),main=&amp;quot;scatterplot3d - 3&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-4-1.png" width="672" /&gt;&#10;&lt;p&gt;来一个比较庸俗的，三维bar图：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;my.mat &amp;lt;- matrix(runif(25), nrow=5)&#10;dimnames(my.mat) &amp;lt;- list(LETTERS[1:5], letters[11:15])&#10;s3d.dat &amp;lt;- data.frame(cols=as.vector(col(my.mat)),rows=as.vector(row(my.mat)),value=as.vector(my.mat))&#10;scatterplot3d(s3d.dat, type=&amp;quot;h&amp;quot;, lwd=5, pch=&amp;quot; &amp;quot;,x.ticklabs=colnames(my.mat),y.ticklabs=rownames(my.mat))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-5-1.png" width="672" /&gt;&#10;&lt;p&gt;上面的可以说都不是很实用，下面看一下二维回归平面图：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;data(trees)&#10;s3d &amp;lt;- scatterplot3d(trees, type=&amp;quot;h&amp;quot;, highlight.3d=TRUE,angle=55, scale.y=0.7, pch=16, main=&amp;quot;scatterplot3d - 5&amp;quot;)&#10;s3d$points3d(seq(10,20,2), seq(85,60,-5), seq(60,10,-10),col=&amp;quot;blue&amp;quot;, type=&amp;quot;h&amp;quot;, pch=16)&#10; # 画回归平面&#10;attach(trees)&#10;my.lm &amp;lt;- lm(Volume ~ Girth + Height)&#10;s3d$plane3d(my.lm, lty.box = &amp;quot;solid&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-6-1.png" width="672" /&gt;&#10;&lt;p&gt;我看见有很多帖子索要二维正态分布的示意图，这里画一个，&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; library(mvtnorm)&#10; x1 &amp;lt;- seq(-10, 10, length = 51)&#10; x2 &amp;lt;- seq(-10, 10, length = 51)&#10; dens &amp;lt;- matrix(dmvnorm(expand.grid(x1,x2),sigma=rbind(c(3,2),c(2,3))),ncol=length(x1))&#10; s3d&amp;lt;-scatterplot3d(x1,x2,seq(min(dens),max(dens),length=length(x1)),&#10; type=&amp;quot;n&amp;quot;,grid=FALSE,angle = 70,&#10; zlab=expression(f(x[1],x[2])),&#10; xlab=expression(x[1]),ylab=expression(x[2]),&#10; main=&amp;quot;Bivariate normal distribution&amp;quot;)&#10; text(s3d$xyz.convert(-1,10,0.07),&#10; labels=expression(f(x)==frac(1,sqrt((2*pi)^n*phantom(&amp;quot;.&amp;quot;)*det(Sigma[X])))*phantom(&amp;quot;.&amp;quot;)*exp(bgroup(&amp;quot;(&amp;quot;,-scriptstyle(frac(1,2)*phantom(&amp;quot;.&amp;quot;))*(x-mu)^T*Sigma[X]^-1*(x-mu),&amp;quot;)&amp;quot;))))&#10; for(i in length(x1):1)&#10; s3d$points3d(rep(x1[i], length(x2)), x2, dens[i,], type = &amp;quot;l&amp;quot;)&#10; for(i in length(x2):1)&#10; s3d$points3d(x1, rep(x2[i], length(x1)), dens[,i], type = &amp;quot;l&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-7-1.png" width="672" /&gt;&#10;&lt;p&gt;之所以画下面这个，完全是因为我贪图色彩：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; cubedraw &amp;lt;- function(res3d, min = 0, max = 255, cex = 2, text. = FALSE)&#10; {&#10; cube01 &amp;lt;- rbind(c(0,0,1), 0, c(1,0,0), c(1,1,0), 1, c(0,1,1), # &amp;lt; 6 outer&#10; c(1,0,1), c(0,1,0)) &#10; cub &amp;lt;- min + (max-min)* cube01&#10; res3d$points3d(cub[c(1:6,1,7,3,7,5) ,], cex = cex, type = 'b', lty = 1)&#10; res3d$points3d(cub[c(2,8,4,8,6), ], cex = cex, type = 'b', lty = 3)&#10; if(text.)&#10; text(res3d$xyz.convert(cub), labels=1:nrow(cub), col='tomato', cex=2)&#10; }&#10; cc &amp;lt;- colors()&#10; crgb &amp;lt;- t(col2rgb(cc))&#10; par(xpd = TRUE)&#10; rr &amp;lt;- scatterplot3d(crgb, color = cc, box = FALSE, angle = 24,&#10; xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))&#10; cubedraw(rr)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-8-1.png" width="672" /&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt; rbc &amp;lt;- rainbow(201)&#10; Rrb &amp;lt;- t(col2rgb(rbc))&#10; rR &amp;lt;- scatterplot3d(Rrb, color = rbc, box = FALSE, angle = 24,&#10; xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))&#10; cubedraw(rR)&#10; rR$points3d(Rrb, col = rbc, pch = 16)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2010/06/13/scatterplot3d/index_files/figure-html/unnamed-chunk-8-2.png" width="672" /&gt;&#10;</description></item></channel></rss>