<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/flip-axes-in-plotly-histograms-few-approaches/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-addict.com/2017/11/29/Flip-axis-plotly-histogram.html"> http://r-addict.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i2.wp.com/r-addict.com/images/fulls/plotly1.jpg?w=456" data-recalc-dims="1"> This seems easy as it requires just to change <code>x</code> parameter to <code>y</code> in the plot specification. Well, there are some edge cases where R users might get a in trouble!</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>plotly</span><span>)</span><span>
</span><span>packageVersion</span><span>(</span><span>'plotly'</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">[1] '4.7.1'</code></pre>
</figure>
<blockquote>
<p>Before you go, let me just explain myself. I have just started learning R interface to <code>plotly</code> library and I am really amazed by the effort people done to make those wires connected and possible to be used for a broad audience.</p>
</blockquote>
<figure>
<pre><code data-lang="r"><span>set.seed</span><span>(</span><span>2</span><span>);</span><span> </span><span>values</span><span> </span><span>&lt;-</span><span> </span><span>rnorm</span><span>(</span><span>50</span><span>)</span><span>
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;default&quot;</span><span>)</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;flipped&quot;</span><span>)</span><span>
</span><span>subplot</span><span>(</span><span>p</span><span>1</span><span>,</span><span> </span><span>p</span><span>2</span><span>)</span></code></pre>
</figure>
<p>What if the plotly object specification is longer than several dozen of lines and one would like to have this feature parametrized in a function’s call? Like in the shiny application, to have the flip as an option?</p>
<p>The quickest solution is to provide an <code>if</code> statement like</p>
<figure>
<pre><code data-lang="r"><span>#' @param values Numeric values to be used in the plot.</span><span>
</span><span>#' @param flip Logical: should the plot be a horizontal histogram?</span><span>
</span><span>to_flip_or_not_to_flip</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>values</span><span>,</span><span> </span><span>flip</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>){</span><span>
</span><span>if</span><span> </span><span>(</span><span>flip</span><span>)</span><span> </span><span>{</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;flipped&quot;</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;default&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>p</span><span>
</span><span>}</span><span>
</span><span>#' @examples</span><span>
</span><span>#' to_flip_or_not_to_flip(p)</span><span>
</span><span>#' to_flip_or_not_to_flip(p, flip = TRUE)</span></code></pre>
</figure>
<p>This is a typical redundancy of the code. Imagine the plot being created in a loop with many extra layers, where in the end the specification is longer than 50 lines. Would you copy and paste all 50 lines just to substitute <code>x</code> with <code>y</code>?</p>
<h1>orientation parameter</h1>
<p>Maybe <code>orientation</code> parameter is an option? After the reference: https://plot.ly/r/reference/#histogram</p>
<p><img src="https://i0.wp.com/r-addict.com/images/fulls/plotly3.jpg?w=456" data-recalc-dims="1"></p>
<figure>
<pre><code data-lang="r"><span>p</span><span>3</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;orient v&quot;</span><span>,</span><span> </span><span>orientation</span><span> </span><span>=</span><span> </span><span>&quot;v&quot;</span><span>)</span><span>
</span><span>p</span><span>4</span><span> </span><span>&lt;-</span><span> </span><span>plot_ly</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;orient h&quot;</span><span>,</span><span> </span><span>orientation</span><span> </span><span>=</span><span> </span><span>&quot;h&quot;</span><span>)</span><span>
</span><span>subplot</span><span>(</span><span>p</span><span>3</span><span>,</span><span> </span><span>p</span><span>4</span><span>)</span></code></pre>
</figure>
<p>one get a wrong plot. <code>values</code> should be assigned to <code>y</code> parameter again.</p>
<p><img src="https://i2.wp.com/r-addict.com/images/fulls/plotly2.jpg?w=456" data-recalc-dims="1"></p>
<p>Of course there is a <a rel="nofollow" href="https://plotly-book.cpsievert.me/" target="_blank">plotly guide book</a> for R users (where I’ve learned <code>subplot()</code>) but one is not going to read the whole documentation just to create a simple horizontal histogram (I suppose).</p>
<h1>The solution?</h1>
<p>One can create the list with all possible parameters that he/she would like to specify (besides default parameters) and then<br>
change the name of <code>x</code> parameter to <code>y</code> in that list depending on flip requirements?</p>
<figure>
<pre><code data-lang="r"><span>parameters</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>values</span><span>,</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;histogram&quot;</span><span>,</span><span>
</span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;x&quot;</span><span>
</span><span>)</span><span>
</span><span>p</span><span>5</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>plot_ly</span><span>,</span><span> </span><span>parameters</span><span>)</span><span>
</span><span># if (flip) {</span><span>
</span><span>names</span><span>(</span><span>parameters</span><span>)[</span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;y&quot;</span><span>
</span><span>parameters</span><span>$</span><span>name</span><span> </span><span>&lt;-</span><span> </span><span>&quot;y&quot;</span><span>
</span><span># }</span><span>
</span><span>p</span><span>6</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>plot_ly</span><span>,</span><span> </span><span>parameters</span><span>)</span><span>
</span><span>subplot</span><span>(</span><span>p</span><span>5</span><span>,</span><span> </span><span>p</span><span>6</span><span>)</span></code></pre>
</figure>
<p><img src="https://i1.wp.com/r-addict.com/images/fulls/plotly4.jpg?w=456" data-recalc-dims="1"></p>
<h1>My personal best?</h1>
<p>Change the object directly after you have specified the plot.<br>
One can easily guess what needs to be changed after looking to <code>str(plot)</code> call.<br>
We would change data attributes and will rename <code>x</code> object to <code>y</code>. See that we can also modify values, not only names of parameters.</p>
<figure>
<pre><code data-lang="r"><span>p</span><span>7</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>5</span><span>
</span><span>str</span><span>(</span><span>p</span><span>7</span><span>$</span><span>x</span><span>$</span><span>attrs</span><span>)</span><span>
</span><span># if (flip) {</span><span>
</span><span>names</span><span>(</span><span>p</span><span>7</span><span>$</span><span>x</span><span>$</span><span>attrs</span><span>[[</span><span>1</span><span>]])[</span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;y&quot;</span><span>
</span><span>p</span><span>7</span><span>$</span><span>x</span><span>$</span><span>attrs</span><span>[[</span><span>1</span><span>]]</span><span>$</span><span>name</span><span> </span><span>&lt;-</span><span> </span><span>&quot;y - change object directly&quot;</span><span>
</span><span># }</span><span>
</span><span>subplot</span><span>(</span><span>p</span><span>5</span><span>,</span><span> </span><span>p</span><span>7</span><span>)</span></code></pre>
</figure>
<p><img src="https://i1.wp.com/r-addict.com/images/fulls/plotly5.jpg?w=456" data-recalc-dims="1"></p>
<h1>Other solutions?</h1>
<p>Do you know cleaner approach? Please don’t hesitate to leave comments <a rel="nofollow" href="http://r-addict.com/2017/11/29/Flip-axis-plotly-histogram.html" target="_blank">at mine blog</a>.</p>
<p>I suppose one could create a plot in <code>ggplot2</code> and then apply <code>ggplotly()</code> function but I am not convinced this function translates all possible further plots to the client ready format, so I’d like to stick to <code>plotly</code> interface.</p>
<blockquote>
<p>Note: sorry for print screens. I could not get interactive results for plotly in the Rmarkdown document compiled with a <a rel="nofollow" href="https://jekyll.yihui.name/2014/09/jekyll-with-knitr.html" target="_blank">jekyll and knitr</a>.</p>
</blockquote>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-addict.com/2017/11/29/Flip-axis-plotly-histogram.html"> http://r-addict.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/8r2-8ON16n8" height="1"></a></div>