<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/how-to-create-interactive-data-visualizations-with-ggvis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/08/10/how-to-create-interactive-data-visualizations-with-ggvis/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-interactive-data-visualizations-with-ggvis"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/coded.jpg" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/coded-150x150.jpg?resize=150%2C150" alt="" class="wp-image-7399" data-recalc-dims="1"></a><strong>INTRODUCTION</strong></p>
<p>The ggvis package is used to make interactive data visualizations. The fact that it combines <a rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/" target="_blank">shiny’s</a> reactive programming model and dplyr’s grammar of data transformation make it a useful tool for data scientists.</p>
<p>This package may allows us to implement features like interactivity, but on the other hand every interactive ggvis plot must be connected to a running R session. </p>
<p><strong>PACKAGE INSTALLATION &amp; DATA FRAME</strong></p>
<p>The first thing you have to do is install and load the ggvis package with:<br>
<code>install.packages(&quot;ggvis&quot;)<br>
library(ggvis)</code></p>
<p>Moreover we need a data set to work with. Tha dataset we chose in our case is “Cars93” which contains data from 93 Cars on Sale in the USA in 1993 and we can find it in the MASS package which of course must be installed and called too. To install and call those packages and attach the “Cars93” dataset use:<br>
<code>install.packages(&quot;MASS&quot;)<br>
library(MASS)<br>
data(&quot;Cars93&quot;)<br>
attach(Cars93)</code></p>
<p>You can use <code>head(Cars93)</code> in order to see the variables of your dataset.</p>
<p>Furthermore you need to install and call the <a rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/" target="_blank">shiny</a> and the magrittr package with:<br>
<code>install.packages(&quot;shiny&quot;)<br>
library(shiny)</code><br>
<code>install.packages(&quot;magrittr&quot;)<br>
library(magrittr)</code></p>
<p><strong>NOTE</strong>: Because of the fact that alla ggvis graphics are web graphics,if you’re not using <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/" target="_blank">RStudio</a> (which provides a built-in browser), you’ll notice that this plot opens in your web browser. </p>
<p><strong>The ggvis() function</strong></p>
<p>The first thing we have to do is call <code>ggvis()</code>. The first argument is the data set that we want to plot, and the second describes which variables we will use. Look at the example below.<br>
<code>plot1 &lt;- ggvis(Cars93, x = ~Length, y = ~Wheelbase)</code></p>
<p>This doesn’t plot anything because you haven’t set how to display your data. The example below creates a scatterplot:<br>
<code>layer_points(plot1)</code></p>
<p>There is an alternative and maybe more practical way to produce the same result with the <code>%>%</code> function of the magrittr package like the example below:<br>
<code>Cars93 %>%<br>
  ggvis(x = ~Length, y = ~Wheelbase) %>%<br>
  layer_points()</code></p>
<p>We use <code>~</code> before the variable name to indicate that we don’t want to literally use the value of the variable, but instead we want we want to use the variable inside in the dataset. We will use it from now on. This is how we can drop <code>x</code> and <code>y</code>.</p>
<p>You can add more variables to the plot by mapping them to other visual properties like <code>fill</code>, <code>stroke</code>, <code>size</code> and <code>shape</code>. Look at the examples below.<br>
<code>Cars93 %>% ggvis(~Length, ~Wheelbase, stroke = ~EngineSize) %>% layer_points()       #stroke<br>
Cars93 %>% ggvis(~Length, ~Wheelbase, fill = ~EngineSize) %>% layer_points()         #fill<br>
Cars93 %>% ggvis(~Length, ~Wheelbase, size = ~EngineSize) %>% layer_points()         #size<br>
Cars93 %>% ggvis(~Length, ~Wheelbase, shape = ~factor(Passengers)) %>% layer_points()#shape </code></p>
<p>If you want to make the points a fixed colour, size or shape, you need to use <code>:=</code> instead of <code>=</code>. Look at the examples below.<br>
<code>Cars93 %>% ggvis(~Length, ~Wheelbase, fill := &quot;yellow&quot;, stroke := &quot;black&quot;) %>% layer_points() #fill<br>
Cars93 %>% ggvis(~Length, ~Wheelbase, size := 350, opacity := 0.5) %>% layer_points()         #size<br>
Cars93 %>% ggvis(~Length, ~Wheelbase, shape := &quot;cross&quot;) %>% layer_points()                    #shape</code></p>
<div>
<aside><img src="http://www.r-exercises.com/2017/08/10/how-to-create-interactive-data-visualizations-with-ggvis/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-interactive-data-visualizations-with-ggvis"></aside>
<div><strong>Learn more</strong> about using ggvis in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/r-complete-data-visualization-solutions/" target="_blank">R: Complete Data Visualization Solutions</a>. In this course you will learn how to:
<ul>
<li>Work extensively with the ggvis package and its functionality</li>
<li>Learn what visualizations exist for your specific use case</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Interaction</strong></p>
<p>You can map visual properties to variables or set them to specific values, but it is far more interesting to connect them to interactive controls. Look at the two sliders’ example below<br>
<code>Cars93 %>%<br>
  ggvis(~Length, ~Wheelbase,<br>
        size := input_slider(1, 100),<br>
        opacity := input_slider(0, 1)<br>
  ) %>%<br>
  layer_points()</code></p>
<p>You can also add interactivity to other plot parameters like the width and centers of histogram bins like the example below:<br>
<code>Cars93%>%<br>
  ggvis(~Length) %>%<br>
  layer_histograms(width =  input_slider(0, 2, step = 0.10, label = &quot;width&quot;),<br>
                   center = input_slider(0, 2, step = 0.05, label = &quot;center&quot;))</code></p>
<p>Except of <code>input_slider()</code>, ggvis also provides <code>input_checkbox()</code>, <code>input_checkboxgroup()</code>, <code>input_numeric()</code>, <code>input_radiobuttons()</code>, <code>input_select()</code> and <code>input_text()</code>.</p>
<p>You can also use keyboard controls with <code>left_right()</code> and <code>up_down()</code>. The following example shows how to control the size of the points by pressing the left and right keyboard controls.<br>
<code>arrow % ggvis(~Length, ~Wheelbase, size := arrow, opacity := 0.5) %>% layer_points()</code></p>
<p>With tooltips you can add more complex interactivity:<br>
<code>Cars93 %>% ggvis(~Length, ~Wheelbase) %>%<br>
  layer_points() %>%<br>
  add_tooltip(function(df) df$Length) </code></p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/02/how-to-create-visualizations-with-iplots-package-in-r/" title="How to create visualizations with iPlots package in R" target="_blank">How to create visualizations with iPlots package in R </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/22/data-wrangling-reshaping/" title="Data wrangling : Reshaping" target="_blank">Data wrangling : Reshaping </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/11/building-shiny-app-exercises-part-10/" title="Building Shiny App exercises part 10" target="_blank">Building Shiny App exercises part 10 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<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-exercises.com/2017/08/10/how-to-create-interactive-data-visualizations-with-ggvis/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-create-interactive-data-visualizations-with-ggvis"> R-exercises</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/De-NDcI8A9E" height="1"></a></div>