<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/" data-count="horizontal" data-text="Trisurf Plots in R using Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post we’ll show how to create Triangular Surface Plots in R. This post is based on <strong>timelyportfolio’s</strong> <a target="_blank" href="https://gist.github.com/timelyportfolio/f71c1ca27d309eb8ec26422fb5fe8bbb#file-data-json">gist</a>.</p>
<div>
<h3>Moebius Strip</h3>
<p>
<pre>library(plotly)
library(geometry)

g &lt;- expand.grid(
  u = seq(0, 2 * pi, length.out = 24),
  v = seq(-1, 1, length.out = 8)
)
tp &lt;- 1 + 0.5 * g$v * cos(g$u / 2)
m &lt;- matrix(
  c(tp * cos(g$u), tp * sin(g$u), 0.5 * g$v * sin(g$u / 2)), 
  ncol = 3, dimnames = list(NULL, c(&quot;x&quot;, &quot;y&quot;, &quot;z&quot;))
)

# the key though is running delaunayn on g rather than m
d &lt;- delaunayn(g)
td &lt;- t(d)
#  but using m for plotting rather than the 2d g

# define layout options
axs &lt;- list(
  backgroundcolor=&quot;rgb(230,230,230)&quot;,
  gridcolor=&quot;rgb(255,255,255)&quot;,
  showbackground=TRUE,
  zerolinecolor=&quot;rgb(255,255,255&quot;
)

# now figure out the colormap
#  start by determining the mean of z for each row
#  of the Delaunay vertices
zmean &lt;- apply(d, MARGIN=1, function(row){mean(m[row,3])})

library(scales)
# result will be slighlty different
#  since colour_ramp uses CIELAB instead of RGB
#  could use colorRamp for exact replication
facecolor = colour_ramp(
  brewer_pal(palette=&quot;RdBu&quot;)(9)
)(rescale(x=zmean))


plot_ly(
  x = m[, 1], y = m[, 2], z = m[, 3],
  # JavaScript is 0 based index so subtract 1
  i = d[, 1]-1, j = d[, 2]-1, k = d[, 3]-1,
  facecolor = facecolor,
  type = &quot;mesh3d&quot;
) %>%
  layout(
    title=&quot;Moebius band triangulation&quot;,
    scene=list(xaxis=axs,yaxis=axs,zaxis=axs),
    aspectratio=list(x=1,y=1,z=0.5)
  )</pre>
<p>
<iframe height="800" width="1024" src="https://plot.ly/~RPlotBot/3008/moebius-band-triangulation.embed"><br>
</iframe>
</div>
<div>
<h3>2D Surface over a disk</h3>
<p>
<pre>n &lt;- 12
h &lt;- 1/(n-1)
r = seq(h, 1, length.out=n)
theta = seq(0, 2*pi, length.out=36)

g &lt;- expand.grid(r=r, theta=theta)

x &lt;- c(g$r * cos(g$theta),0)
y &lt;- c(g$r * sin(g$theta),0)
z &lt;- sin(x*y)

m &lt;- matrix(
  c(x,y,z), 
  ncol = 3,
  dimnames = list(NULL, c(&quot;x&quot;, &quot;y&quot;, &quot;z&quot;))
)

tri &lt;- delaunayn(m[,1:2])

# now figure out the colormap
zmean &lt;- apply(tri,MARGIN=1,function(row){mean(m[row,3])})

library(scales)
library(rje)
facecolor = colour_ramp(
  cubeHelix(12)
)(rescale(x=zmean))

plot_ly(
  x=x, y=y, z=z,
  i=tri[,1]-1, j=tri[,2]-1, k=tri[,3]-1,
  facecolor=facecolor,
  type=&quot;mesh3d&quot;
) %>%
  layout(
    title=&quot;Triangulated surface&quot;,
    scene=list(
      xaxis=axs,
      yaxis=axs,
      zaxis=axs,
      camera=list(
        eye=list(x=1.75,y=-0.7,z=0.75)
      )
    ),
    aspectratio=list(x=1,y=1,z=0.5)
  )</pre>
<p>
<iframe height="800" width="1024" src="https://plot.ly/~RPlotBot/3010/triangulated-surface.embed"><br>
</iframe>
</div>
<div>
<h3>Chopper from python</h3>
<p>
<pre>library(geomorph)
plyFile &lt;- 'http://people.sc.fsu.edu/~jburkardt/data/ply/chopper.ply'
dest &lt;- basename(plyFile)
if (!file.exists(dest)) {
  download.file(plyFile, dest)
}

mesh &lt;- read.ply(dest)
# see getS3method(&quot;shade3d&quot;, &quot;mesh3d&quot;) for details on how to plot 

# plot point cloud
x &lt;- mesh$vb[&quot;xpts&quot;,]
y &lt;- mesh$vb[&quot;ypts&quot;,]
z &lt;- mesh$vb[&quot;zpts&quot;,]
m &lt;- matrix(c(x,y,z), ncol=3, dimnames=list(NULL,c(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)))

# now figure out the colormap
zmean &lt;- apply(t(mesh$it),MARGIN=1,function(row){mean(m[row,3])})

library(scales)
facecolor = colour_ramp(
  brewer_pal(palette=&quot;RdBu&quot;)(9)
)(rescale(x=zmean))

plot_ly(
  x = x, y = y, z = z,
  i = mesh$it[1,]-1, j = mesh$it[2,]-1, k = mesh$it[3,]-1,
  facecolor = facecolor,
  type = &quot;mesh3d&quot;
)</pre>
<p>
<iframe height="800" width="1024" src="https://plot.ly/~RPlotBot/3012/y-vs-x.embed"><br>
</iframe>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/" data-counturl="http://www.r-bloggers.com/trisurf-plots-in-r-using-plotly/" data-count="vertical" data-text="Trisurf Plots in R using Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_dMg7l9a5tY"></a></div>