Skip to content

Instantly share code, notes, and snippets.

@msund
Last active August 29, 2015 14:09
Show Gist options
  • Save msund/f52f7fc40946913553ff to your computer and use it in GitHub Desktop.
Save msund/f52f7fc40946913553ff to your computer and use it in GitHub Desktop.
R bloggers, 3D
<p><a href="https://plot.ly">Plotly</a> is a platform for data analysis, graphing, and collaboration. You can use <a href="https://plot.ly/ggplot2">ggplot2</a>, Plotly's <a href="https://plot.ly/r">R API</a>, Plotly's web app to make and share interactive plots. The latest: you can make <a href="http://blog.plot.ly/post/101360048217/7-plotly-graphs-in-3d-stocks-cats-and-lakes">3D plots</a>. Below are a few examples; this post shows three others, made with ggplot2 and Plotly's R API.</p>
<br>
<br>
<center><a href="http://blog.plot.ly/post/101360048217/7-plotly-graphs-in-3d-stocks-cats-and-lakes"><img src="http://i.imgur.com/asHsC9x.png" title="3D" /></a></center>
<br>
<br>
First, let's convert a ggplot2 tile plane into a Plotly graph, then convert it to a 3D plot. You can copy and paste this code and use a test username and key, or <a href="https://plot.ly/ssu">sign up for an account</a> and generate your own.
<br>
<br>
<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;"><a href="http://inside-r.org/r-doc/utils/install.packages"><span style="color: #003399; font-weight: bold;">install.packages</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;devtools&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;"># so we can install from github</span>
devtools<span style="">::</span>install_github<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ropensci/plotly&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;"># plotly is part of ropensci</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">&#40;</span>plotly<span style="color: #009900;">&#41;</span>
&nbsp;
py <span style="">&lt;-</span> plotly<span style="color: #009900;">&#40;</span>username=<span style="color: #0000ff;">&quot;r_user_guide&quot;</span><span style="color: #339933;">,</span> key=<span style="color: #0000ff;">&quot;mw5isa4yqp&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;"># open plotly connection</span>
&nbsp;
pp <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: #009900;">&#40;</span>n<span style="color: #339933;">,</span>r=<span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
x <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">&#40;</span><span style="">-</span>r<span style="">*</span><span style="color: #000000; font-weight: bold;">pi</span><span style="color: #339933;">,</span> r<span style="">*</span><span style="color: #000000; font-weight: bold;">pi</span><span style="color: #339933;">,</span> len=n<span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/expand.grid"><span style="color: #003399; font-weight: bold;">expand.grid</span></a><span style="color: #009900;">&#40;</span>x=x<span style="color: #339933;">,</span> y=x<span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>r <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>x<span style="">^</span><span style="color: #cc66cc;">2</span> <span style="">+</span> <a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>y<span style="">^</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>z <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/cos"><span style="color: #003399; font-weight: bold;">cos</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>r<span style="">^</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="">*</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">&#40;</span><span style="">-</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="">$</span>r<span style="">/</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>
<span style="color: #009900;">&#125;</span>
p <span style="">&lt;-</span> <a href="http://inside-r.org/packages/cran/ggplot"><span style="">ggplot</span></a><span style="color: #009900;">&#40;</span>pp<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> aes<span style="color: #009900;">&#40;</span>x=x<span style="color: #339933;">,</span>y=y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
p <span style="">&lt;-</span> p <span style="">+</span> geom_tile<span style="color: #009900;">&#40;</span>aes<span style="color: #009900;">&#40;</span>fill=z<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
py<span style="">$</span>ggplotly<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span></pre></div></div>
<br>
<br>
We return a URL: <a href="https://plot.ly/~r_user_guide/83/y-vs-x/">plot.ly/~r_user_guide/83/y-vs-x/</a>. The URL hosts the interactive plot, rendered with D3.js, a JavaScript visualization library. Each plot stores the data, and code to reproduce a plot with MATLAB, Python, R, Julia, and JavaScript.
<br>
<br>
<a href="https://plot.ly/~r_user_guide/83/y-vs-x/"><img src="http://i.imgur.com/jz75xIH.png" title="ggplot2" /></a>
<br>
<br>
We can export or embed plots in <a href="www.r-bloggers.com/how-to-publish-r-and-ggplot2-to-the-web/">Shiny Apps, knitr, Slidify,</a> blogs, and in an iframe, as we're doing below.
<br>
<br>
<iframe width="640" height="480" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~r_user_guide/83.embed?width=640&height=480"></iframe>
<br>
<br>
We'll now style, share, and change to 3D in the web app. Press the "Fork and Edit" button <a href="https://plot.ly/83/~r_user_guide/">to get started in the GUI</a>. The web app runs online and is free, so you won't need to install or download anything.
<br>
<br>
<a href="https://plot.ly/83/~r_user_guide/"><img src="http://i.imgur.com/RTWQOjH.png" title="ggplot2" /></a>
<br>
<br>
Below is our edited plot. Go to <a href="https://plot.ly/~MattSundquist/2260">the plot</a> and press " View full-size graph" to really dig in, or go straight to a full-screen version: <a href="https://plot.ly/~MattSundquist/2260.embed">plot.ly/~MattSundquist/2260.embed</a>. Try clicking, holding, and toggling to flip, drag, and zoom. Press the icons in the upper right-hand corner to change modes.
<br>
<br>
<iframe width="640" height="480" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~MattSundquist/2260.embed?width=640&height=480"></iframe>
<br>
<br>
We can <a href="http://docs.ggplot2.org/current/geom_tile.html">also use qplot</a> and edit the plot in the GUI. Assuming you ran the code above:
<br>
<br>
<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;">qplot<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=pp<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> geom=<span style="color: #0000ff;">&quot;tile&quot;</span><span style="color: #339933;">,</span> fill=z<span style="color: #009900;">&#41;</span>
&nbsp;
py<span style="">$</span>ggplotly<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>
<br>
<br>
<iframe width="640" height="480" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~MattSundquist/2264.embed?width=640&height=480"></iframe>
<br>
<br>
We can also make 3D plots from a grid of imported Excel, Dropbox, Google Drive, or pasted data. Or combine data from another plot. <a href="https://plot.ly/r/privacy/">Privacy settings</a> are like Google Drive and GitHub: you control if plots are public or private, you own your data and can add collaborators, or you can <a href="https://plot.ly/product/enterprise/">run Plotly on-premise</a>.
<br>
<br>
<center><img src="https://31.media.tumblr.com/898a5df0539b156b86b317444d07672b/tumblr_inline_nbhlz33qTr1suclhk.png"></center>
<br>
<br>
To create a 3D plot directly from R, we can use Plotly's R API. For example, try this code to make a surface plot. Plotly also <a href="https://plot.ly/r/">supports 3D line and scatter plots</a>.
<br>
<br>
<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;"><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> <span style="">&lt;-</span> <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span>
<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span>
z = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="">-</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Monday&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Tuesday&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Wednesday&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Thursday&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Friday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
y = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Morning&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Afternoon&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Evening&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
type = <span style="color: #0000ff;">&quot;surface&quot;</span>
<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>
response <span style="">&lt;-</span> py<span style="">$</span>plotly<span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> kwargs=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">&#40;</span>filename=<span style="color: #0000ff;">&quot;3D surface&quot;</span><span style="color: #339933;">,</span> fileopt=<span style="color: #0000ff;">&quot;overwrite&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<a href="http://inside-r.org/r-doc/base/url"><span style="color: #003399; font-weight: bold;">url</span></a> <span style="">&lt;-</span> response<span style="">$</span>url <span style="color: #666666; font-style: italic;"># returns plot URL</span></pre></div></div>
<br>
<br>
Editing in the GUI offers options for editing the lighting, lines that appear when hovering, colors in the scene and grid lines, and axis titles.
<br>
<br>
<a href="https://plot.ly/~MattSundquist/2263"><img src="http://i.imgur.com/oiPRCxg.png" title="3D" /></a>
<br>
<br>
The plot, as well as code to reproduce our new version, is also available at the URL: <a href="https://plot.ly/~MattSundquist/2263">plot.ly/~MattSundquist/2263</a>.
<br>
<br>
<iframe width="640" height="480" frameborder="0" seamless="seamless" scrolling="no" src="https://plot.ly/~MattSundquist/2263.embed?width=640&height=480"></iframe>
<br>
<br>
3D plots can be useful for showing three-dimensional spaces like <a href="https://plot.ly/~matlab_user_guide/2079/">world</a>, lakes, or <a href="https://plot.ly/r/3d-surface-plots/">mountains</a>. You can also plot a <a href="https://plot.ly/r/3d-line-plots/">random walk</a>, the <a href="http://blog.plot.ly/post/101360048217/7-plotly-graphs-in-3d-stocks-cats-and-lakes">Lorenz attractor</a>, functions, and <a href="http://blog.plot.ly/post/101360048217/7-plotly-graphs-in-3d-stocks-cats-and-lakes">stock volatility</a>. We welcome your feedback, thoughts, and suggestions. We're at feedback at plot.ly and <a href="https://twitter.com/plotlygraphs">@plotlygraphs</a>. Happy plotting!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment