<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/get-the-best-from-ggplotly/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-graph-gallery.com/2017/06/07/get-the-best-from-ggplotly/"> Blog – The R graph Gallery</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="" src="https://i1.wp.com/www.r-graph-gallery.com/wp-content/uploads/2017/06/Demo_ggplotly.gif?w=450" class="wp-image-3610" data-recalc-dims="1"><a rel="nofollow" href="https://plot.ly/r/" target="_blank">Plotly</a> is an R library allowing to make amazing <a rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/interactive-r-graphics/" target="_blank">interactive charts</a> in a minute. This blog post shows how to get the best from <strong>ggplotly</strong>, the function of plotly allowing you to turn any <a rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/ggplot2-package/" target="_blank">ggplot2 chart</a> interactive. A special care is given on features that are not present in ggplot2 (like <strong>hovering</strong>), and features that are not well translated by ggplotly (like l<strong>egend position</strong>).<span></span></p>
<p>Let’s start with a usual ggplot2 chart. Note that the <a rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/ggplot2-package/" target="_blank">ggplot2 section</a> of the <a rel="nofollow" href="http://www.r-graph-gallery.com/" target="_blank">R graph gallery</a> contains dozens of examples with the reproducible code, so you will most likely find the graphic you need to visualize your data.</p>
<pre># Libraries
library(tidyverse)
library(plotly)

# Scatterplot
p=ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + 
    geom_point(size=6, alpha=0.6)
p</pre>
<p><img sizes="(max-width: 480px) 100vw, 480px" src="https://i2.wp.com/www.r-graph-gallery.com/wp-content/uploads/2017/06/Best_from_ggplotly_1.png?w=450" alt="" class="wp-image-3607" data-recalc-dims="1"></p>
<p>Then, we can apply the magic of plotly in just one line of code! We get an interactive chart on which we can <strong>zoom</strong>, <strong>hover</strong>, <strong>play with axis</strong>, <strong>export</strong>, click on legend to make <strong>group appear</strong>/disappear … This is great and works with any ggplot2 graphic.</p>
<pre>ggplotly(p)</pre>
<p><iframe width="450" src="http://plot.ly/~holtzy/88.embed"></iframe></p>
<p>Since ggplot2 creates static charts, it does not offer any option to custom hover text. This can be done as follow: <strong>1/</strong> create a text vector with the text you want to show <strong>2/</strong> use <strong>plotly_build()</strong> to make the interactive chart <strong>3/</strong> pass the text to the interactive version with <strong>style()</strong></p>
<pre>mytext=paste(&quot;Sepal Length = &quot;, iris$Sepal.Length, &quot;\n&quot; , &quot;Sepal Width = &quot;, iris$Sepal.Width, &quot;\n&quot;, &quot;Row Number: &quot;,rownames(iris),  sep=&quot;&quot;)    
pp=plotly_build(p)   
style( pp, text=mytext, hoverinfo = &quot;text&quot;, traces = c(1, 2, 3) )</pre>
<p><iframe width="450" src="http://plot.ly/~holtzy/90.embed"></iframe></p>
<p>Unfortunately, some ggplot2 parameters are sometimes misunderstood by ggplotly. For example, let’s place the ggplot2 legend inside the plot:</p>
<pre>p_changed=ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + 
    geom_point(size=6, alpha=0.6) + 
    theme(legend.position = c(.83, .9))

p_changed</pre>
<p><img sizes="(max-width: 480px) 100vw, 480px" src="https://i0.wp.com/www.r-graph-gallery.com/wp-content/uploads/2017/06/Best_from_ggplotly_4.png?w=450" alt="" class="wp-image-3608" data-recalc-dims="1"></p>
<p>Using a basic ggplotly() call will not change the legend position. To do so, we need to enter parameters using the real plotly syntax as follow. Note that all plotly arguments can be found <a rel="nofollow" href="https://plot.ly/r/reference/" target="_blank">here</a>.</p>
<pre>pp_changed=plotly_build(p_changed)   
style( pp_changed ) %>% 
	layout( legend = list(x = 0.01, y = 0.95) )</pre>
<p><iframe width="450" src="http://plot.ly/~holtzy/94.embed"></iframe></p>
<p>Note that the <a rel="nofollow" href="http://www.r-graph-gallery.com/" target="_blank">R graph gallery</a> offers a lot of other examples of <a rel="nofollow" href="http://www.r-graph-gallery.com/portfolio/interactive-r-graphics/" target="_blank">interactive R charts</a>. You can follow the gallery on <a rel="nofollow" href="https://twitter.com/R_Graph_Gallery" target="_blank">Twitter</a> or on <a rel="nofollow" href="https://www.facebook.com/RGraphGallery/" target="_blank">Facebook</a> to be notified of recent updates.</p>
<p><a rel="nofollow" href="http://www.r-graph-gallery.com/2017/06/07/get-the-best-from-ggplotly/#" title="Recommend this" target="_blank"><span>1</span> <span></span></a></p>
	<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://www.r-graph-gallery.com/2017/06/07/get-the-best-from-ggplotly/"> Blog – The R graph Gallery</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/FKoVAXGVOk8" height="1"></a></div>