<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/where-to-live-in-the-us/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/11/16/wheretoliveus/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I was fascinated by this <a rel="nofollow" href="https://xkcd.com/1916/" target="_blank">xkcd comic</a> about where to live based on your temperature preferences. I also thought it’d be fun to try to make a similar one from my R session! Since I’m no meteorologist and was a bit unsure of how to define winter and summer, and of their relevance in countries like, say, India which has monsoon, I decided to focus on a single country located in one hemisphere only and big enough to offer some variety… the USA! So, dear American readers, where should you live based on your temperature preferences?</p>
<p><span></span></p>
<h1>Defining data sources</h1>
<h2>Weather data</h2>
<p>The data for the original xkcd graph comes from <a rel="nofollow" href="http://www.weatherbase.com/" target="_blank">weatherbase</a>. I changed sources because 1) I was not patient enough to wait for weatherbase to send me a custom dataset which I imagine is what xkcd author did and 2) I’m the creator of <a rel="nofollow" href="http://ropensci.github.io/riem/" target="_blank">a cool package accessing airport weather data</a> for the whole word including the US! My package is called “riem” like “R Iowa Environmental Mesonet” (the source of the data, a <a rel="nofollow" href="https://mesonet.agron.iastate.edu/request/download.phtml?network=IN__ASOS" target="_blank">fantastic website</a>) and “we laugh” in Catalan (at the time I wrote the package I was living in Barcelona and taking 4 hours of Catalan classes a week!). It’s a simple but good package which underwent <a rel="nofollow" href="https://github.com/ropensci/onboarding/issues/39" target="_blank">peer-review</a> at rOpenSci onboarding, thank you <a rel="nofollow" href="https://twitter.com/gbwanderson" target="_blank">Brooke</a>!</p>
<p>I based my graph on data from the last winter and the last summer. I reckon that one should average over more years, but nothing important is at stake here, right?</p>
<h2>Cities sample</h2>
<p>My package has a function for downloading weather data for a given airport based on its ID. For instance Los Angeles airport is LAX. At that point I just needed a list of cities in the US with their airport code. Indeed with my package you can get all airport weather networks, one per US state, and all the stations withing that network… this is a lot of airports with no way to automatically determine how big they are! And to get the city name since it’d be so hard to parse the airport name I’d have resorted to geocoding with <a rel="nofollow" href="https://github.com/ropensci/opencage" target="_blank">e.g. this package</a>. A bit complicated for a simple fun graph!</p>
<p>So I went to the <a rel="nofollow" href="https://en.wikipedia.org/wiki/List_of_the_busiest_airports_in_the_United_States" target="_blank">Wikipedia page of the busiest airports in the US</a> and ended up getting <a rel="nofollow" href="https://www.bts.gov/content/passengers-boarded-top-50-us-airports" target="_blank">this dataset</a> from the US Department of Transportation (such a weird open data format by the way… I basically copy-pasted the first two columns in a spreadsheet!). This is not perfect, getting a list of major cities in every state would be more fair but hey reader I want you to live in a really big city so that I might know where it is.</p>
<h2>Ok so let’s get the data</h2>
<div>
<div>
<pre><code><span>us_airports</span><span> </span><span>&lt;-</span><span> </span><span>readr</span><span>::</span><span>read_csv</span><span>(</span><span>&quot;data/2017-11-16_wheretoliveus_airports.csv&quot;</span><span>)</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>us_airports</span><span>[</span><span>1</span><span>:</span><span>10</span><span>,])</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Code</th>
</tr>
</thead>
<tbody>
<tr>
<td>Atlanta, GA (Hartsfield-Jackson Atlanta International)</td>
<td>ATL</td>
</tr>
<tr>
<td>Los Angeles, CA (Los Angeles International)</td>
<td>LAX</td>
</tr>
<tr>
<td>Chicago, IL (Chicago O’Hare International)</td>
<td>ORD</td>
</tr>
<tr>
<td>Dallas/Fort Worth, TX (Dallas/Fort Worth International)</td>
<td>DFW</td>
</tr>
<tr>
<td>New York, NY (John F. Kennedy International)</td>
<td>JFK</td>
</tr>
<tr>
<td>Denver, CO (Denver International)</td>
<td>DEN</td>
</tr>
<tr>
<td>San Francisco, CA (San Francisco International)</td>
<td>SFO</td>
</tr>
<tr>
<td>Charlotte, NC (Charlotte Douglas International)</td>
<td>CLT</td>
</tr>
<tr>
<td>Las Vegas, NV (McCarran International)</td>
<td>LAS</td>
</tr>
<tr>
<td>Phoenix, AZ (Phoenix Sky Harbor International)</td>
<td>PHX</td>
</tr>
</tbody>
</table>
<p>I first opened my table of 50 airports. Then, I made the call to the Iowa Environment Mesonet.</p>
<div>
<div>
<pre><code><span>summer_weather</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>us_airports</span><span>$</span><span>Code</span><span>,</span><span> </span><span>riem</span><span>::</span><span>riem_measures</span><span>,</span><span>
</span><span>date_start</span><span> </span><span>=</span><span> </span><span>&quot;2017-06-01&quot;</span><span>,</span><span>
</span><span>date_end</span><span> </span><span>=</span><span> </span><span>&quot;2017-08-31&quot;</span><span>)</span><span>
</span><span>winter_weather</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map_df</span><span>(</span><span>us_airports</span><span>$</span><span>Code</span><span>,</span><span> </span><span>riem</span><span>::</span><span>riem_measures</span><span>,</span><span>
</span><span>date_start</span><span> </span><span>=</span><span> </span><span>&quot;2016-12-01&quot;</span><span>,</span><span>
</span><span>date_end</span><span> </span><span>=</span><span> </span><span>&quot;2017-02-28&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>We then remove the lines that will be useless for further computations (The xkcd graph uses average temperature in the winter and average Humidex in the summer which uses both temperature and <a rel="nofollow" href="https://en.wikipedia.org/wiki/Dew_point" target="_blank">dew point</a>).</p>
<div>
<div>
<pre><code><span>
</span><span>summer_weather</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>summer_weather</span><span>,</span><span>
</span><span>!</span><span>is.na</span><span>(</span><span>tmpf</span><span>),</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>dwpf</span><span>))</span><span>
</span><span>winter_weather</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>winter_weather</span><span>,</span><span>
</span><span>!</span><span>is.na</span><span>(</span><span>tmpf</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>I quickly checked that there was “nearly no missing data” so I didn’t remove any station nor day but if I were doing this analysis for something serious I’d do more checks including the time difference between measures for instance. Note that I end up with 48 airports only, there was no measure for Honolulu, HI (Honolulu International), San Juan, PR (Luis Munoz Marin International). Too bad!</p>
<h1>Calculating the two weather values</h1>
<p>I started by converting all temperatures to Celsius degrees because although I like you, American readers, Fahrenheit degrees do not mean anything to me which is problematic for checking results plausibility for instance. I was lazy and just used Brooke Anderson’s (yep the same Brooke who reviewed <code>riem</code> for rOpenSci) <a rel="nofollow" href="https://github.com/geanders/weathermetrics/" target="_blank"><code>weathermetrics</code> package</a>.</p>
<div>
<div>
<pre><code><span>summer_weather</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>summer_weather</span><span>,</span><span>
</span><span>tmpc</span><span> </span><span>=</span><span> </span><span>weathermetrics</span><span>::</span><span>convert_temperature</span><span>(</span><span>tmpf</span><span>,</span><span> </span><span>old_metric</span><span> </span><span>=</span><span> </span><span>&quot;f&quot;</span><span>,</span><span> </span><span>new_metric</span><span> </span><span>=</span><span> </span><span>&quot;c&quot;</span><span>),</span><span>
</span><span>dwpc</span><span> </span><span>=</span><span> </span><span>weathermetrics</span><span>::</span><span>convert_temperature</span><span>(</span><span>dwpf</span><span>,</span><span> </span><span>old_metric</span><span> </span><span>=</span><span> </span><span>&quot;f&quot;</span><span>,</span><span> </span><span>new_metric</span><span> </span><span>=</span><span> </span><span>&quot;c&quot;</span><span>))</span><span> </span><span>winter_weather</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>winter_weather</span><span>,</span><span>
</span><span>tmpc</span><span> </span><span>=</span><span> </span><span>weathermetrics</span><span>::</span><span>convert_temperature</span><span>(</span><span>tmpf</span><span>,</span><span> </span><span>old_metric</span><span> </span><span>=</span><span> </span><span>&quot;f&quot;</span><span>,</span><span> </span><span>new_metric</span><span> </span><span>=</span><span> </span><span>&quot;c&quot;</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<h2>Summer values</h2>
<p>Summer values are Humidex values. The author explained Humidex “combines heat and <a rel="nofollow" href="https://en.wikipedia.org/wiki/Dew_point" target="_blank">dew point</a>”. Once again I went to <a rel="nofollow" href="https://en.wikipedia.org/wiki/Humidex" target="_blank">Wikipedia</a> and found the formula. I’m not in a mood to fiddle with formula writing on the blog so please go there if you want to see it. There’s also a package with a <a rel="nofollow" href="https://www.rdocumentation.org/packages/comf/versions/0.1.7/topics/calcHumidity?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">function returning the Humidex</a>. I was feeling adventurous and therefore wrote my own function and checked the results with the numbers from Wikipedia. It was first wrong because I wrote a “+” instead of a “-“… checking one’s code is crucial.</p>
<div>
<div>
<pre><code><span>calculate_humidex</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>temp</span><span>,</span><span> </span><span>dewpoint</span><span>){</span><span>
</span><span>temp</span><span> </span><span>+</span><span> </span><span>0.5555</span><span>*</span><span>(</span><span>6.11</span><span> </span><span>*</span><span>exp</span><span>(</span><span>5417.7530</span><span>*</span><span>(</span><span>1</span><span>/</span><span>273.16</span><span> </span><span>-</span><span> </span><span>1</span><span>/</span><span>(</span><span>273.15</span><span> </span><span>+</span><span> </span><span>dewpoint</span><span>)))</span><span> </span><span>-</span><span> </span><span>10</span><span>)</span><span>
</span><span>}</span><span>
</span><span>calculate_humidex</span><span>(</span><span>30</span><span>,</span><span> </span><span>15</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 33.969
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>calculate_humidex</span><span>(</span><span>30</span><span>,</span><span> </span><span>25</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] 42.33841
</code></pre>
</div>
</div>
<p>And then calculating the summer Humidex values by station was quite straightforward…</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>summer_weather</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>summer_weather</span><span>,</span><span>
</span><span>humidex</span><span> </span><span>=</span><span> </span><span>calculate_humidex</span><span>(</span><span>tmpc</span><span>,</span><span> </span><span>dwpc</span><span>))</span><span>
</span><span>summer_values</span><span> </span><span>&lt;-</span><span> </span><span>summer_weather</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>station</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>summarise</span><span>(</span><span>summer_humidex</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>humidex</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>… as it was for winter temperatures.</p>
<div>
<div>
<pre><code><span>winter_values</span><span> </span><span>&lt;-</span><span> </span><span>winter_weather</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>station</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>summarise</span><span>(</span><span>winter_tmpc</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>tmpc</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<h1>Prepare data for plotting</h1>
<p>I first joined winter and summer values</p>
<div>
<div>
<pre><code><span>climates</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>winter_values</span><span>,</span><span> </span><span>summer_values</span><span>,</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;station&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Then I re-added city airport names.</p>
<div>
<div>
<pre><code><span>climates</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>left_join</span><span>(</span><span>climates</span><span>,</span><span> </span><span>us_airports</span><span>,</span><span>
</span><span>by</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;station&quot;</span><span> </span><span>=</span><span> </span><span>&quot;Code&quot;</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>I only kept the city name.</p>
<div>
<div>
<pre><code><span>head</span><span>(</span><span>climates</span><span>$</span><span>Name</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] &quot;Atlanta, GA (Hartsfield-Jackson Atlanta International)&quot;              
## [2] &quot;Austin, TX (Austin - Bergstrom International)&quot;                       
## [3] &quot;Nashville, TN (Nashville International)&quot;                             
## [4] &quot;Boston, MA (Logan International)&quot;                                    
## [5] &quot;Baltimore, MD (Baltimore/Washington International Thurgood Marshall)&quot;
## [6] &quot;Cleveland, OH (Cleveland-Hopkins International)&quot;
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>climates</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>climates</span><span>,</span><span> </span><span>city</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>Name</span><span>,</span><span> </span><span>&quot; \\(.*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>))</span><span>
</span><span>head</span><span>(</span><span>climates</span><span>$</span><span>city</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] &quot;Atlanta, GA&quot;   &quot;Austin, TX&quot;    &quot;Nashville, TN&quot; &quot;Boston, MA&quot;   
## [5] &quot;Baltimore, MD&quot; &quot;Cleveland, OH&quot;
</code></pre>
</div>
</div>
<h1>Plotting!</h1>
<p>When imitating an xkcd graph, one should use the <code>xkcd</code> package! I had already done that <a rel="nofollow" href="http://www.masalmon.eu/2017/05/14/evergreenreviewgraph/" target="_blank">in this post</a>.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;xkcd&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;extrafont&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;ggrepel&quot;</span><span>)</span><span>
</span><span>xrange</span><span> </span><span>&lt;-</span><span> </span><span>range</span><span>(</span><span>climates</span><span>$</span><span>summer_humidex</span><span>)</span><span>
</span><span>yrange</span><span> </span><span>&lt;-</span><span> </span><span>range</span><span>(</span><span>climates</span><span>$</span><span>winter_tmpc</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>climates</span><span>,</span><span>
</span><span>aes</span><span>(</span><span>summer_humidex</span><span>,</span><span> </span><span>winter_tmpc</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_text_repel</span><span>(</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>city</span><span>),</span><span>
</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;xkcd&quot;</span><span>,</span><span> </span><span>max.iter</span><span> </span><span>=</span><span> </span><span>50000</span><span>)</span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Where to live based on your temperature preferences&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Data from airports weather stations, 2016-2017&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Summer heat and humidity via Humidex&quot;</span><span>)</span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Winter temperature in Celsius degrees&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>xkcdaxis</span><span>(</span><span>xrange</span><span> </span><span>=</span><span> </span><span>xrange</span><span>,</span><span>
</span><span>yrange</span><span> </span><span>=</span><span> </span><span>yrange</span><span>)</span><span>+</span><span>
</span><span>theme_xkcd</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>16</span><span>,</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;xkcd&quot;</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-10" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-11-16-wheretoliveus/unnamed-chunk-10-1.png?w=456" data-recalc-dims="1"></p>
<p>So, which city seems attractive to you based on this plot? Or would you use different weather measures, e.g. do you care about rain?</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.masalmon.eu/2017/11/16/wheretoliveus/"> Maëlle</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/VNIvWAcdnk8" height="1"></a></div>