<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/growth-of-datafest-over-the-years/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/05/24/growth-of-datafest-over-the-years/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <br>





<br>
<br>
<br>
</p>
<p>In <a rel="nofollow" href="https://rviews.rstudio.com/2017/04/05/datafestorg/" target="_blank">a previous post</a>, I introduced DataFest and how one can streamline the organization of this event using Google Forms and tools from the tidyverse. In this post, I’ll walk through building a Shiny app that demonstrates the growth of DataFest over the years, both in terms of host locations and participating institutions, as well as in terms of the number of students who participated in each event.</p>
<p>Here is a list of all packages used in this article:</p>
<pre><code>library(tidyverse)
library(googlesheets)
library(devtools)
library(ggmap)
library(stringr)
library(leaflet)</code></pre>
<p>The data were contributed by the event organizers, and were collected using <a rel="nofollow" href="https://goo.gl/forms/jlLC9B7aVIaF1QoQ2" target="_blank">a Google Form</a>.</p>
<p>To begin, the data are read using the <code>googlesheets</code> package.</p>
<pre><code>datafest_wide &lt;- gs_title(&quot;DataFest over the years (Responses)&quot;) %>%
  gs_read()</code></pre>
<div>
<h2>Data prep</h2>
<p>Then minimal manipulation is applied to column names, and a new column concatenating city, state, and country is added to be used in geocoding.</p>
<pre><code># rename columns
yrs &lt;- sort(rep(2011:2017, 3))
cols &lt;- c(&quot;df_&quot;, &quot;num_part_&quot;, &quot;other_inst_&quot;)

names(datafest_wide) &lt;- c(&quot;timestamp&quot;, &quot;host&quot;, &quot;city&quot;, &quot;state&quot;, &quot;country&quot;, &quot;url&quot;,
                     paste0(cols, yrs))

# geocode host location
datafest_wide &lt;- datafest_wide %>%
  mutate(address = paste(city, state, country)) %>% 
  mutate_geocode(address)</code></pre>
<p>Note that we need to use the development version of the <code>ggmap</code> package for <code>mutate_geocode()</code> to play nicely with a <code>tbl_df</code>. You can install this version with <code>install_github(&quot;dkahle/ggmap&quot;)</code>.</p>
<p>Next, we convert the data from wide to long format using functionality from the <code>tidyr</code> package. First, we gather the columns that contain yearly information (for each year, we have an indicator for whether an event was hosted at the location, the number of students that participated, and other participating institutions, if any). Then, we strip the year information from variable names, and instead save it as a variable in the dataset. Finally, we spread the key-value pair across three columns.</p>
<pre><code>datafest_long &lt;- datafest_wide %>% 
  gather(key, value, df_2011:other_inst_2017) %>%
  mutate(year = as.numeric(str_match(key, &quot;[0-9]+&quot;))) %>%
  mutate(key = str_replace(key, &quot;_[0-9]+&quot;, &quot;&quot;)) %>%
  spread(key, value) %>%
  mutate(num_part = as.numeric(num_part))</code></pre>
</div>
<div>
<h2>Map of 2017 ASA DataFests</h2>
<p>The eventual goal of this post is to make a Shiny app that maps DataFest spread and growth over the years; however, I’ll start by making a map for just one year, 2017, to develop the code for the map, and then use this code within a Shiny app.</p>
<p>Going forward, I’ll refer to the long dataset as <code>datafest</code>.</p>
<pre><code>datafest &lt;- datafest_long</code></pre>
<p>First, I take a subset of the data for hosts that held an event in 2017:</p>
<pre><code>datafest_2017 &lt;- filter(datafest, year == 2017 &amp; df == &quot;Yes&quot;)</code></pre>
<p>Then, I set a few colors to be used in the plot,</p>
<pre><code>href_color &lt;- &quot;#A7C6C6&quot;
marker_color &lt;- &quot;black&quot;
part_color &lt;- &quot;#89548A&quot;</code></pre>
<p>as well as the bounds of the plot based on the min/max longitude/latitude.</p>
<pre><code>left &lt;- floor(min(datafest$lon))
right &lt;- ceiling(max(datafest$lon))
bottom &lt;- floor(min(datafest$lat))
top &lt;- ceiling(max(datafest$lat))</code></pre>
<p>I will be making the map using the <code>leaflet</code> package, as this package allows for easily overlaying markers and popups to maps. The popups are text bubbles that appear when a point is clicked, and that contain additional information about that data point. This is a good place to add some event-specific information, such as name of host, and link to their event homepage, other participating institutions (if any), and number of participants.</p>
<pre><code>host_text &lt;- paste0(
  &quot;<b><a rel="nofollow" href="https://www.r-bloggers.com/growth-of-datafest-over-the-years/%27%22," target="_blank">&quot;, datafest_2017$host, &quot;</a></b>&quot;
)

other_inst_text &lt;- paste0(
  ifelse(is.na(datafest_2017$other_inst), 
         &quot;&quot;, 
         paste0(&quot;<br>&quot;, &quot;with participation from &quot;, datafest_2017$other_inst))
)

part_text &lt;- paste0(
  &quot;<font color=", part_color,">&quot;, datafest_2017$num_part, &quot; participants</font>&quot;
)

popups &lt;- paste0(
  host_text, other_inst_text, &quot;<br>&quot;, part_text
)</code></pre>
<p>We’re finally ready to make our map! Note that the radii of the points are proportional to the log of the number of participants (times an arbitrary factor for visual appeal).</p>
<pre><code>leaflet() %>%
  addTiles() %>%
  fitBounds(lng1 = left, lat1 = bottom, lng2 = right, lat2 = top) %>%
  addCircleMarkers(lng = datafest_2017$lon, lat = datafest_2017$lat,
                   radius = log(datafest_2017$num_part) * 1.2, 
                   fillColor = marker_color,
                   color = marker_color,
                   weight = 1,
                   fillOpacity = 0.5,
                   popup = popups)</code></pre>
<div></div>
<p>
</div>
<div>
<h2>Shiny app</h2>
<p>Next, we build upon our earlier plot to create a Shiny app that has the following three components:</p>
<ol>
<li>A slider input with animation for values between 2011 and 2017 (DataFest years, so far)</li>
<li>A line plot that shows the increase in the number participants over the year</li>
<li>A map that shows the spread of DataFest geographically over the years</li>
</ol>
<p>You can find and interact with the app at <a rel="nofollow" href="https://gallery.shinyapps.io/datafest-map-all-years/" target="_blank">https://gallery.shinyapps.io/datafest-map-all-years/</a>, and the code for the app, as well as all steps up to this point, can be found at <a rel="nofollow" href="https://github.com/mine-cetinkaya-rundel/datafest-growth" target="_blank">this GitHub repo</a>.</p>
<p><a rel="nofollow" href="https://gallery.shinyapps.io/datafest-map-all-years/" target="_blank"> <img alt="Screenshot of app" src="https://i2.wp.com/rviews.rstudio.com/post/2017-05-22-growth-of-datafest-over-the-years_files/app.png?w=456&ssl=1" data-recalc-dims="1"> </a></p>
</div>
<p> </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="https://rviews.rstudio.com/2017/05/24/growth-of-datafest-over-the-years/"> R Views</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/yipIpG4CW50" height="1"></a></div>