<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/a-graphic-look-at-bay-area-bike-share/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.nycdatascience.com/student-works/r-shiny/graphic-look-bay-area-bike-share/"> R – NYC Data Science Academy Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2><a name="introduction"></a><span>Introduction</span></h2>
<p>This post is about the first of the four projects we are supposed to deliver at the <a target="_blank" rel="nofollow" href="http://nycdatascience.com/data-science-bootcamp/">NYC Data Science Academy Data Science Bootcamp</a> program. The requirements were:</p>
<blockquote><p>For this project, you are not only expected to lead your audience through data insights<br>
gleaned from visual representations, but also required to build an interactive Shiny<br>
application that can create and modify such graphics on-the-fly. You must find the<br>
balance of the correct level of interactivity and simplicity that allows an outside user to<br>
both understand your points and also uncover their own insights without getting<br>
distracted by excess accoutrements.</p>
</blockquote>
<p>I chose to work on data provided by <a target="_blank" rel="nofollow" href="http://www.bayareabikeshare.com/">Bay Area Bike Share</a>. This is a “bike sharing system with 700 bikes and 70 stations across the [Bay Area] region […]. Intended to provide Bay Area residents and visitors with an additional transportation option for getting around the region, the Bay Area Bike Share is fun, easy, and affordable.”</p>
<p><span></span><br>
<a name="sourceData"></a></p>
<h2><span>Source Data</span></h2>
<p>I sourced the data from the <a target="_blank" rel="nofollow" href="http://www.bayareabikeshare.com/open-data">Bay Area Bike Share Open Data page</a> and downloaded the datasets for year 2 (Sep 2014 – Aug 2015) and year 3 (Sep 2015 – Aug 2016) in order to create a new dataset containing all the data for the year 2015. The following data was used in this project:</p>
<p><strong>Stations</strong></p>
<table>
<tbody>
<tr>
<td>station_id</td>
<td>name</td>
<td>lat</td>
<td>long</td>
<td>dockcount</td>
<td>landmark</td>
<td>installation</td>
</tr>
</tbody>
</table>
<p><strong>Trips</strong></p>
<table>
<tbody>
<tr>
<td>Trips ID</td>
<td>Duration</td>
<td>Start Date</td>
<td>Start Station</td>
<td>Start Terminal</td>
<td>End Date</td>
<td>End Station</td>
<td>End Terminal</td>
<td>Bike No</td>
<td>Subscriber Type</td>
<td>ZIP Code</td>
</tr>
</tbody>
</table>
<p><strong>Weather</strong></p>
<table>
<tbody>
<tr>
<td>ZIP</td>
<td>Date</td>
<td>Max. Temperature F</td>
<td>Mean Temperature F</td>
<td>Min. Temperature F</td>
<td>Cloud Cover</td>
</tr>
</tbody>
</table>
<p>The data was in very good quality so that I had to do almost no cleaning. For the temperature there were a few missing values for one day where there was no data recorded. Because this data was not too important for the overall application but I did not want to have gaps in the data, I used the <span>zoo</span> library to copy values that already existed for the a variable to the empty cells of the same variable. All of the other data was basically 100% complete so that this part could be indeed kept to a minimum. All the data was converted from CSV to the for R optimized RDS data format to save space and improve the loading speed in R.</p>
<p><a name="dataAggregates"></a></p>
<h2><span>Data Aggregates</span></h2>
<p>Having established the data source to use for my project and acquainted myself with it, I created various aggregations per dataset (cf. <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/student-works/r-shiny/graphic-look-bay-area-bike-share/#sourceData">Source Data</a>) to be able to calculate various measurements on different groupings of the data. They are:</p>
<p><strong>Based on Trips</strong></p>
<ul>
<li><span>routes</span>: Grouping of trips per route, i.e. <span>Start Station</span> to <span>End Station</span>, with various measurements, such as <span>total amount</span> of trips taken on that route,  <span>durations</span> (<span>min</span>, <span>max</span>, <span>avg</span>) of these trips, …, for both the cases that direction does and does not matter, i.e. <span>Start</span> – <span>End</span> != <span>End</span> – <span>Start</span> and <span>Start</span> – <span>End</span> = <span>End</span> – <span>Start</span>.</li>
<li><span>bikes</span>: Grouping of trips per bike number with various measurements, such as <span>total amount</span> of trips taken on that bike or the <span>number of days</span> each bike was in use.</li>
<li><span>stations by start/end hour/date</span>: Usage of stations as start or end point of a trip by <span>hour of day</span> or <span>day of year</span> in total amount of trips.</li>
<li><span>zipStartByDate</span>: <span>Total amount</span> of trips per <span>day of year</span> the trip was started on and <span>ZIP</span> of the station the trip was started at.</li>
<li><span>cust</span>: <span>Total amount</span> and <span>total duration</span> of trips per <span>subscriber type</span>, i.e. Subscriber (annual or 30-day member) or Customer (24-hour or 3-day member).</li>
</ul>
<p><strong>Based on Stations</strong></p>
<ul>
<li><span>docks</span>: Grouping of stations per city (<span>landmark</span>) with the measurements <span>stationCount</span> and <span>dockCount</span>.</li>
</ul>
<p><strong>Based on Weather</strong></p>
<ul>
<li><span>weatherTrips</span>: Weather per <span>day of year</span> and <span>ZIP</span> in combination with the<span> amount of trips</span> from <span>zipStartByDate</span>.</li>
</ul>
<p><a name="visualization"></a></p>
<h2><span>Visualization in Shiny</span></h2>
<p>The visualization of the data had to be done in <a target="_blank" rel="nofollow" href="http://shiny.rstudio.com/">Shiny</a>, which is a “web application framework for R [to] turn […] analyses into interactive web applications”. Furthermore, I used <a target="_blank" rel="nofollow" href="https://rstudio.github.io/shinydashboard/">Shiny Dashboard</a> to create a management dashboard-esque application.</p>
<p><strong>Start</strong></p>
<p>As you can see on the image below, on the menu on the left I created various items to thematically group the charts and maps I was going to implement. These are Stations, Trips, Bikes, Customers, and Weather (cf. <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/student-works/r-shiny/graphic-look-bay-area-bike-share/#dataAggregates">Data Aggregates</a>). I will go on to explain them in detail.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-start-01.png"><img sizes="(max-width: 1024px) 100vw, 1024px" alt="Start" class="wp-image-18642" src="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-start-01-1024x534.png?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Start</p>
</div>
<p><strong>Stations</strong></p>
<p>This tab delivers various insights into the data on station level. It starts with showing a map where all the stations are shown at their actual position. The overview tab then shows the user in which city how many stations and docks are positioned.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-stations.gif"><img alt="Stations" class="wp-image-18646" src="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-stations.gif?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Stations</p>
</div>
<p>For each of these stations, detailed statistics about their usage either throughout the year, i.e. per date, or throughout the day, i.e. on a 24h clock, can be found. The stations can also be compared in terms of the aforementioned characteristics.</p>
<p><strong>Trips</strong></p>
<p>The next item on the menu is the trips tab. This allows access to detailed statistics about all the trips taken on the Bay Area Bike Share network. It begins with the map the user already knows from the stations tab, but this time they are able to print routes on the map, for both the cases that direction does and does not matter. The default threshold for routes to display is set to 1,000, i.e. there must have been at least 1,000 trips on a route for it to be drawn on the map. The user can change this setting. The line width corresponds to the amount of trips taken on a certain route.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i1.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-trips.gif"><img alt="Trips" class="wp-image-18648" src="https://i1.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/01/sh-proj-01-trips.gif?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Trips</p>
</div>
<p>The <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Sankey_diagram">sankey chart</a> on the next tab gives a good overview of the most important routes and stations of the system by indicating by means of line width and node width from left to right – start to end – which trips were popular in the year 2015. Finally, the table shows various statistics for each of the routes.</p>
<p><strong>Bikes</strong></p>
<p>In terms of bikes, the user is presented with a histogram that shows bike usage in three metrics – total amount of usage, total duration of usage and median duration of usage. It becomes immediately clear that some bikes are heavily used while others seem to be barely touched at all.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-bikes.gif"><img alt="Bikes" class="wp-image-18649" src="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-bikes.gif?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Bikes</p>
</div>
<p>The timeline on the next tab displays for each bike exactly how many days it was in operation in 2015, i.e. during which time period it was used for trips. The dual slider on top lets the user filter the range of operation days which the timeline should show.</p>
<p><strong>Customers</strong></p>
<p>The content of this tab does not react to any user input. It basically shows a bunch of value boxes that compare the two customer types, Subscriber and Customer.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-cust-01.png"><img sizes="(max-width: 1582px) 100vw, 1582px" alt="Customers" class="wp-image-18650" src="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-cust-01.png?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Customers</p>
</div>
<p><strong>Weather</strong></p>
<p>This tab allows the user to take a look at the temperatures in F, C, and the cloud cover per day of the year for each of the five cities and compare it to the trips started on that day on any of the stations in each city.</p>
<div class="wp-caption"><a target="_blank" rel="nofollow" href="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-weather.gif"><img alt="Weather" class="wp-image-18652" src="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/sh-proj-01-weather.gif?resize=456%2C238" data-recalc-dims="1"></a>
<p class="wp-caption-text">Weather</p>
</div>
<p><a name="conclusion"></a></p>
<h2><span>Conclusion</span></h2>
<p>In terms of actual findings within the data, nothing was too surprising:</p>
<ul>
<li>Some routes are more popular than others</li>
<li>In cities with more stations there is a higher frequency of trips taken on the network</li>
<li>It seems to be the case that trips to and from Caltrain stations are more popular than others</li>
<li>Longer trips are taken by ad-hoc customers – tourists (?) –  than subscribers, who are probably mostly commuters</li>
<li>A bit astonishing: not all bikes seem to be in use the whole year. Some were only on the streets for a few days while others were used throughout the whole year.</li>
<li>Temperature and cloud cover do not seem to affect the amount of trips taken at all. Although there was data about on which days it rained or where there even was a thunderstorm, it was not taken into consideration as it was only listed per day and broken not down to hours of the day.</li>
</ul>
<p>This dataset, although only consisting of a few files, makes for a good showcase about the possibilities of a Shiny application. Basic capabilities – info and values boxes, and <a target="_blank" rel="nofollow" href="http://ggplot2.org/">ggplot</a> charts – were used as well as more advanced features, such as <a target="_blank" rel="nofollow" href="https://developers.google.com/chart/interactive/docs/gallery">Google Charts</a> and <a target="_blank" rel="nofollow" href="https://rstudio.github.io/leaflet/">Leaflet Maps</a>. It was a valuable experience to get an overall look at R this way – loading and working with data, creating various plots in different engines, and combining it all together into a single dashboard. As for Shiny in particular, this project helped me very much in understanding how to exactly use the different files <span>ui.R</span>, <span>server.R</span>, <span>global.R</span> and <span>helpers.R</span>, and how to create an app out of all these building blocks.</p>
<p>As stated above, code and data can be found <a target="_blank" rel="nofollow" href="https://github.com/Steeefan/nycdsa-proj-01">on GitHub,</a> while the app itself is <a target="_blank" rel="nofollow" href="https://steeefan.shinyapps.io/bay_area_bike_share/">online at shinyapps.io</a>.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/student-works/r-shiny/graphic-look-bay-area-bike-share/">A Graphic Look at Bay Area Bike Share</a> appeared first on <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/">NYC Data Science Academy Blog</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://blog.nycdatascience.com/student-works/r-shiny/graphic-look-bay-area-bike-share/"> R – NYC Data Science Academy Blog</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/3Fl1auTR2-E"></a></div>