Where to live in Japan: XKCD-themed climate plots and maps!

[This article was first published on R by R(yo), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the past week or so, XKCD graphs of “The most comfortable place to live in ___” have been popping up on the #rstats community on Twitter. Jumping onto this trend (though slightly late), I will do one for Japan, the country where I was born! Some great examples that I’ve seen so far include Maëlle Salmon’s blog post for cities in the USA (which started this trend), as well as ones for Spain, Germany, Netherlands, and even for all of Europe! The original XKCD comic can be seen here.

Let’s get started!

<span class="n">library</span><span class="p">(</span><span class="n">riem</span><span class="p">)</span><span class="w">

</span><span class="n">riem_stations</span><span class="p">(</span><span class="n">network</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"JP__ASOS"</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 136 x 4
##       id             name      lon      lat
##    <chr>            <chr>    <dbl>    <dbl>
##  1  RORA AGUNI ISLAND     127.2403 26.59277
##  2  RJOE AKENO (JASDF)    136.6722 34.53333
##  3  RJSK AKITA AIRPORT    140.2167 39.61667
##  4  RJKA AMAMI AIRPORT    129.7125 28.43083
##  5  RJSA AOMORI AIRPORT   140.6886 40.73333
##  6  RJEC ASAHIKAWA AIRPOR 142.4475 43.67083
##  7  RJCA ASAHIKAWA (JASDF 142.3650 43.79444
##  8  RJFA ASHIYA (JASDF)   130.6517 33.88139
##  9  RJTA ATSUGI NAS (JMSD 139.4500 35.45472
## 10  RJAO CHICHIJIMA ISLAN 142.1900 27.09167
## # ... with 126 more rows

Using Maëlle Salmon’s riem package we can look up weather data from all over the world collected at airport weather stations. We can see that there are 136 different stations (and airports) in Japan’s network! From the ID we can also see that the airports are coded in the ICAO standard rather than the IATA standard, this will come into play later!

For which exact cities to choose, I took a note from Maëlle’s article where she searched for the cities with the busiest airports. This makes sense as the weather data we will gather and the airport data will then have a similar column (the ICAO code) on which we can join on!

My source was from the Ministry of Land, Infrastructure, Transport and Tourism (MLIT) statistics on Japan’s busiest airports (in terms of total passenger traffic), which has been copied onto everybody’s favorite “knowledge bank”, Wikipedia (link)! The most recent list is from 2015 and although I could look up more recent figures I don’t think two years would make such a big difference (… and I’m lazy).

I load up my favorite web scraping package, rvest and gather the list of airports! (For a step-by-step intro to web-scraping using R, check out my other blog post here!)

<span class="n">library</span><span class="p">(</span><span class="n">rvest</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">

</span><span class="n">url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"https://en.wikipedia.org/wiki/List_of_the_busiest_airports_in_Japan"</span><span class="w">

</span><span class="c1"># css selector: "table.wikitable:nth-child(10)"</span><span class="w">

</span><span class="n">japan_airports</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">url</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">read_html</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_node</span><span class="p">(</span><span class="s2">"table.wikitable:nth-child(10)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_table</span><span class="p">()</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">japan_airports</span><span class="p">)</span><span class="w">
</span>
## Observations: 51
## Variables: 8
## $ Rank           <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ...
## $ Airport        <chr> "Haneda Airport", "Narita International Airport...
## $ `City\nserved` <chr> "Tokyo", "Tokyo", "Osaka", "Fukuoka", "Sapporo"...
## $ Region         <chr> "Kanto", "Kanto", "Kansai", "Kyushu", "Hokkaido...
## $ `IATA/ICAO`    <chr> "HND/RJTT", "NRT/RJAA", "KIX/RJBB", "FUK/RJFF",...
## $ Passengers     <chr> "75,254,942", "34,751,221", "23,136,223", "20,9...
## $ Aircraft       <chr> "438,542", "233,500", "163,506", "172,964", "14...
## $ Cargo          <chr> "1,173,752", "2,122,318", "745,606", "262,481",...

In the table, the airport codes in both IATA and ICAO format are in a single column separated by a slash. We can use the separate() function from the tidyr package to separate the airport code column into the respective IATA and ICAO codes. The regex for a literal slash is "\\/", remember that you need to escape twice!

Let’s also rename City\nserved into a simpler City.

Then, let’s save our handy dataset so we do not have to re-run web scraping every time!

<span class="c1"># Separate IATA and ICAO codes:</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w"> </span><span class="c1"># use separate() function!</span><span class="w">

</span><span class="n">japan_airports</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">japan_airports</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">separate</span><span class="p">(</span><span class="n">`IATA/ICAO`</span><span class="p">,</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"IATA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"ICAO"</span><span class="p">),</span><span class="w"> </span><span class="s2">"\\/"</span><span class="p">)</span><span class="w">

</span><span class="n">japan_airports</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">japan_airports</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">rename</span><span class="p">(</span><span class="n">City</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`City\nserved`</span><span class="p">)</span><span class="w">

</span><span class="n">write.csv</span><span class="p">(</span><span class="n">japan_airports</span><span class="p">,</span><span class="w"> </span><span class="s2">"~/R_materials/japan_weather_xkcd/japan_airports.csv"</span><span class="p">)</span><span class="w">
</span>

So, if we start a new session we can use read.csv() to load our data set without have to go through the web scraping code!

<span class="n">japan_airports</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read.csv</span><span class="p">(</span><span class="s2">"~/R_materials/japan_weather_xkcd/japan_airports.csv"</span><span class="p">,</span><span class="w"> 
         </span><span class="n">stringsAsFactors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">

</span><span class="c1"># We only need a small subset of the variables for this particular blog post!</span><span class="w">
</span><span class="n">jp_airport_codes</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">japan_airports</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">select</span><span class="p">(</span><span class="n">Airport</span><span class="p">,</span><span class="w"> </span><span class="n">City</span><span class="p">,</span><span class="w"> </span><span class="n">ICAO</span><span class="p">)</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">jp_airport_codes</span><span class="p">)</span><span class="w">
</span>
## Observations: 51
## Variables: 3
## $ Airport <chr> "Haneda Airport", "Narita International Airport", "Kan...
## $ City    <chr> "Tokyo", "Tokyo", "Osaka", "Fukuoka", "Sapporo", "Naha...
## $ ICAO    <chr> "RJTT", "RJAA", "RJBB", "RJFF", "RJCC", "ROAH", "RJOO"...

Weather data

For the weather data I considered quite a few sources before just settling with the riem package for convenience.

  • The first place I looked was the Japan Meteorological Agency’s website in both English and Japanese. If you go here you can find the the climatological tables for all Japanese airports in English. You can download the entire data set as a .zip file (click on “File” on the top header) or you can click on each airport (with its corresponding ICAO location indicator) to grab its report.
  • You can also find in English, here, the tables of monthly climate statistics with drop-down menus for each station and for different variables such as “monthly mean air temperature”, “monthly vapor pressure”, etc.
  • Probably the most useful tables for a quick glance is here, which shows tables for monthly and annual climate normals for the major observatories in Japan from 1981-2010.
  • For Japanese people there is a built-in API on the website (here!) that lets you choose from a multitude of different options and download your own customized dataset as a CSV file.

Just putting this out there for anybody who wants to look at Japan’s meteorological data in the future!

Now to grab the weather data from the weather stations using the riem package, we use code from Maëlle Salmon’s original post where we use map_df() to basically to apply riem_measures() on each airport code (as a vector input) with the output being a nice data frame! I’ve been slowly trying to incorporate more purrr into my data analysis tool set, so it’s great that I get another chance to see it in action here!

<span class="n">library</span><span class="p">(</span><span class="n">purrr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">riem</span><span class="p">)</span><span class="w">

</span><span class="n">summer_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map_df</span><span class="p">(</span><span class="n">jp_airport_codes</span><span class="o">$</span><span class="n">ICAO</span><span class="p">,</span><span class="w"> </span><span class="n">riem_measures</span><span class="p">,</span><span class="w">
                                </span><span class="n">date_start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2017-06-01"</span><span class="p">,</span><span class="w">
                                </span><span class="n">date_end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2017-08-31"</span><span class="p">)</span><span class="w">

</span><span class="n">winter_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map_df</span><span class="p">(</span><span class="n">jp_airport_codes</span><span class="o">$</span><span class="n">ICAO</span><span class="p">,</span><span class="w"> </span><span class="n">riem_measures</span><span class="p">,</span><span class="w">
                                </span><span class="n">date_start</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2016-12-01"</span><span class="p">,</span><span class="w">
                                </span><span class="n">date_end</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"2017-02-28"</span><span class="p">)</span><span class="w">

</span><span class="c1"># As before we save these as .csv files:</span><span class="w">
</span><span class="n">write.csv</span><span class="p">(</span><span class="n">summer_weather</span><span class="p">,</span><span class="w"> </span><span class="s2">"~/R_materials/japan_weather_xkcd/summer_weather.csv"</span><span class="p">)</span><span class="w">
</span><span class="n">write.csv</span><span class="p">(</span><span class="n">winter_weather</span><span class="p">,</span><span class="w"> </span><span class="s2">"~/R_materials/japan_weather_xkcd/winter_weather.csv"</span><span class="p">)</span><span class="w">

</span><span class="c1"># Load them back in after opening a new session instead of waiting to download from RIEM package again:</span><span class="w">
</span><span class="n">summer_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read.csv</span><span class="p">(</span><span class="s2">"summer_weather.csv"</span><span class="p">,</span><span class="w"> </span><span class="n">stringsAsFactors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span><span class="n">winter_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read.csv</span><span class="p">(</span><span class="s2">"winter_weather.csv"</span><span class="p">,</span><span class="w"> </span><span class="n">stringsAsFactors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w">
</span>

The result is two gigantic datasets of around 105,000 observations each along with 25 variables ranging from dew point temperature, wind speed in knots, relative humidity, and many more!

Further data munging

Recalculate the temperatures into Celcius using the convert_temperature() function in the weathermetrics package!

A quick reminder that the formula is:

<span class="n">library</span><span class="p">(</span><span class="n">weathermetrics</span><span class="p">)</span><span class="w">

</span><span class="n">summer_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">summer_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                    </span><span class="n">mutate</span><span class="p">(</span><span class="n">tmpc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">convert_temperature</span><span class="p">(</span><span class="n">tmpf</span><span class="p">,</span><span class="w">
                                                      </span><span class="n">old_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"f"</span><span class="p">,</span><span class="w"> 
                                                      </span><span class="n">new_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"c"</span><span class="p">),</span><span class="w">
                           </span><span class="n">dwpc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">convert_temperature</span><span class="p">(</span><span class="n">dwpf</span><span class="p">,</span><span class="w">
                                                      </span><span class="n">old_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"f"</span><span class="p">,</span><span class="w">
                                                      </span><span class="n">new_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"c"</span><span class="p">))</span><span class="w">

</span><span class="n">winter_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">winter_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">tmpc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">convert_temperature</span><span class="p">(</span><span class="n">tmpf</span><span class="p">,</span><span class="w">
                                    </span><span class="n">old_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"f"</span><span class="p">,</span><span class="w"> 
                                    </span><span class="n">new_metric</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"c"</span><span class="p">))</span><span class="w">
</span>

We can then calculate the Humidex with the calcHumx() function from the comf package!

For those curious the formula is:

The interpretation of the humidex is as follows:

  • 20-29: Little to no discomfort
  • 30-39: Some discomfort
  • 40-45: Great discomfort (avoid exertion)
  • 45+ : Dangerous (possibility of heat stroke)
<span class="n">library</span><span class="p">(</span><span class="n">comf</span><span class="p">)</span><span class="w">

</span><span class="n">summer_weather</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">summer_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                    </span><span class="n">mutate</span><span class="p">(</span><span class="n">humidex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">calcHumx</span><span class="p">(</span><span class="n">ta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmpc</span><span class="p">,</span><span class="w"> </span><span class="n">rh</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dwpc</span><span class="p">))</span><span class="w">

</span><span class="n">summer_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">summer_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                 </span><span class="n">group_by</span><span class="p">(</span><span class="n">station</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                 </span><span class="n">summarize</span><span class="p">(</span><span class="n">summer_avg_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">tmpc</span><span class="p">,</span><span class="w"> </span><span class="n">na.rm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">),</span><span class="w">
                           </span><span class="n">summer_humidex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">humidex</span><span class="p">,</span><span class="w"> </span><span class="n">na.rm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">))</span><span class="w">

</span><span class="n">winter_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">winter_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                 </span><span class="n">group_by</span><span class="p">(</span><span class="n">station</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
                 </span><span class="n">summarize</span><span class="p">(</span><span class="n">winter_avg_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">tmpc</span><span class="p">,</span><span class="w"> </span><span class="n">na.rm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">))</span><span class="w">
</span>

Join summer and winter together using the left_join() operation! Then, join with the jp_airport_codes data set to add in city names to airport code data set, combine them by the airport ICAO code.

<span class="n">climate_japan</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">left_join</span><span class="p">(</span><span class="n">summer_data</span><span class="p">,</span><span class="w"> </span><span class="n">winter_data</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"station"</span><span class="p">)</span><span class="w">

</span><span class="n">climate_japan</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">left_join</span><span class="p">(</span><span class="n">climate_japan</span><span class="p">,</span><span class="w"> </span><span class="n">jp_airport_codes</span><span class="p">,</span><span class="w">
                           </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"station"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"ICAO"</span><span class="p">))</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">climate_japan</span><span class="p">)</span><span class="w">
</span>
## Observations: 48
## Variables: 6
## $ station         <chr> "RJAA", "RJAH", "RJBB", "RJBE", "RJCB", "RJCC"...
## $ summer_avg_temp <dbl> 24.20110, 23.46787, 26.08489, 26.42125, 18.908...
## $ summer_humidex  <dbl> 22.32085, 21.24909, 24.80028, 25.49317, 15.330...
## $ winter_avg_temp <dbl> 5.5732117, 4.9624521, 8.2165839, 8.0681648, -5...
## $ Airport         <chr> "Narita International Airport", "Ibaraki Airpo...
## $ City            <chr> "Tokyo", "Tokyo", "Osaka", "Kobe", "Obihiro", ...

Looking at what we have… we can remove Narita Airport’s data as not it’s not really Tokyo, it’s located far out in Chiba, and besides the airport there’s nothing of interest (to tourists at least) out there anyways! Haneda Airport’s data is more representative of Tokyo’s weather, although it is stuck out in Tokyo Bay.

Also we need to remove Kansai International Airport, as it is around 20 miles away from all the attractions in Osaka. Fun fact: Kansai Int’l was built to relieve congestion in Osaka Airport (also included in this data set) and only handles international flights!

<span class="n">climate_japan</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">climate_japan</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="n">station</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"RJAA"</span><span class="p">)</span><span class="w">    </span><span class="c1"># RJAA == Narita Airport</span><span class="w">

</span><span class="n">climate_japan</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">climate_japan</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="n">station</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"RJBB"</span><span class="p">)</span><span class="w">    </span><span class="c1"># RJBB == Kansai Int'l Airport</span><span class="w">
</span>

For some odd reason Ibaraki Airport is listed as Tokyo, when it is anything but! Also, we should shorten Kochi’s label, the plot is going to be cluttered enough as it is!

<span class="n">climate_japan</span><span class="o">$</span><span class="n">City</span><span class="p">[</span><span class="n">climate_japan</span><span class="o">$</span><span class="n">station</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"RJAH"</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Ibaraki"</span><span class="w">   </span><span class="c1"># Tokyo >>> Ibaraki</span><span class="w">

</span><span class="n">climate_japan</span><span class="o">$</span><span class="n">City</span><span class="p">[</span><span class="n">climate_japan</span><span class="o">$</span><span class="n">station</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"RJOK"</span><span class="p">]</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"Kochi"</span><span class="w">     </span><span class="c1"># Kochi, Kochi >>> Kochi</span><span class="w">
</span>

Iwakuni appears twice for some reason… very odd, but we can remove it by using the unique() function which filters out any duplicate rows.

<span class="n">climate_japan</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">climate_japan</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">unique</span><span class="p">()</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">climate_japan</span><span class="p">)</span><span class="w">
</span>
## Observations: 45
## Variables: 6
## $ station         <chr> "RJAH", "RJBE", "RJCB", "RJCC", "RJCH", "RJCK"...
## $ summer_avg_temp <dbl> 23.46787, 26.42125, 18.90891, 18.28105, 19.112...
## $ summer_humidex  <dbl> 21.24909, 25.49317, 15.33031, 14.82229, 15.733...
## $ winter_avg_temp <dbl> 4.9624521, 8.0681648, -5.6058944, -4.8474507, ...
## $ Airport         <chr> "Ibaraki Airport", "Kobe Airport", "Obihiro Ai...
## $ City            <chr> "Ibaraki", "Kobe", "Obihiro", "Sapporo", "Hako...

Great! Our data set looks ready!

Plotting!

Well, before actually plotting, we have to install the xkcd font and package. You can read about how to install it here.

<span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggrepel</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">xkcd</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">extrafont</span><span class="p">)</span><span class="w">

</span><span class="n">xrange</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">range</span><span class="p">(</span><span class="n">climate_japan</span><span class="o">$</span><span class="n">summer_humidex</span><span class="p">)</span><span class="w">
</span><span class="n">yrange</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">range</span><span class="p">(</span><span class="n">climate_japan</span><span class="o">$</span><span class="n">winter_avg_temp</span><span class="p">)</span><span class="w">

</span><span class="n">set.seed</span><span class="p">(</span><span class="m">8</span><span class="p">)</span><span class="w">

</span><span class="n">climate_japan</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">summer_humidex</span><span class="p">,</span><span class="w"> </span><span class="n">winter_avg_temp</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_text_repel</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">City</span><span class="p">),</span><span class="w"> 
                  </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w">
                  </span><span class="n">max.iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">50000</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ggtitle</span><span class="p">(</span><span class="s2">"Where to live in Japan based on your temperature preferences"</span><span class="p">,</span><span class="w">
          </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Data from airport weather stations 2016-2017"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">xlab</span><span class="p">(</span><span class="s2">"Humidex: summer heat and humidity"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ylab</span><span class="p">(</span><span class="s2">"Avg. winter temperature in Celsius"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">xkcdaxis</span><span class="p">(</span><span class="n">xrange</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">xrange</span><span class="p">,</span><span class="w">
           </span><span class="n">yrange</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">yrange</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_xkcd</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"text"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.25</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Hokkaido Island"</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"segment"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.25</span><span class="p">,</span><span class="w"> </span><span class="n">yend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-5.5</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16</span><span class="p">,</span><span class="w"> </span><span class="n">xend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">17.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"segment"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-4.25</span><span class="p">,</span><span class="w"> </span><span class="n">yend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">-6.5</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">11.75</span><span class="p">,</span><span class="w"> </span><span class="n">xend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">13.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"blue"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"text"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">25</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">20</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Okinawa and \nRyukyu Islands"</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"segment"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">17.5</span><span class="p">,</span><span class="w"> </span><span class="n">yend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">15</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">26</span><span class="p">,</span><span class="w"> </span><span class="n">xend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">29.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"darkred"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"segment"</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">22</span><span class="p">,</span><span class="w"> </span><span class="n">yend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">22</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">27</span><span class="p">,</span><span class="w"> </span><span class="n">xend</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">30.5</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"darkred"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">annotate</span><span class="p">(</span><span class="s2">"text"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">16.5</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Honshu, Kyushu, \n and Shikoku Islands"</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5.5</span><span class="p">)</span><span class="w">
</span>

The graph looks like a map of Japan… with cities in Hokkaido on the bottom left instead of the top right, vice-versa for Okinawa! Let’s try plotting our data onto an actual map of Japan to get a different perspective.

Weather data on the map of Japan

First we need to acquire the longitude and latitude data for Japan with the map_data() function in ggplot2. We also need to add in the coordinate data into our climate_japan dataset.

<span class="n">JPN</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map_data</span><span class="p">(</span><span class="n">map</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"world"</span><span class="p">,</span><span class="w"> </span><span class="n">region</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Japan"</span><span class="p">)</span><span class="w">

</span><span class="n">lat_lon</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">summer_weather</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">station</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">summarize</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">lat</span><span class="p">),</span><span class="w"> </span><span class="n">lon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">lon</span><span class="p">))</span><span class="w">

</span><span class="n">climate_japan_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">left_join</span><span class="p">(</span><span class="n">climate_japan</span><span class="p">,</span><span class="w"> </span><span class="n">lat_lon</span><span class="p">,</span><span class="w"> </span><span class="n">by</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"station"</span><span class="p">)</span><span class="w"> 

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">climate_japan_map</span><span class="p">)</span><span class="w">
</span>
## Observations: 45
## Variables: 8
## $ station         <chr> "RJAH", "RJBE", "RJCB", "RJCC", "RJCH", "RJCK"...
## $ summer_avg_temp <dbl> 23.46787, 26.42125, 18.90891, 18.28105, 19.112...
## $ summer_humidex  <dbl> 21.24909, 25.49317, 15.33031, 14.82229, 15.733...
## $ winter_avg_temp <dbl> 4.9624521, 8.0681648, -5.6058944, -4.8474507, ...
## $ Airport         <chr> "Ibaraki Airport", "Kobe Airport", "Obihiro Ai...
## $ City            <chr> "Ibaraki", "Kobe", "Obihiro", "Sapporo", "Hako...
## $ lat             <dbl> 36.1817, 34.6328, 42.7333, 42.7742, 41.7700, 4...
## $ lon             <dbl> 140.4147, 135.2239, 143.2172, 141.6926, 140.82...

To be able to view the map better, we can cut out the Okinawa and Ryukyu Islands to create space by filtering out latitudes below 30°. We will create a separate map for those islands too.

<span class="c1"># Winter avg. temp: ####</span><span class="w">

</span><span class="n">climate_japan_map</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">30</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">lat</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">winter_avg_temp</span><span class="p">),</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_text_repel</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">City</span><span class="p">),</span><span class="w">
                  </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4.5</span><span class="p">,</span><span class="w">
                  </span><span class="n">max.iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">50000</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_polygon</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">JPN</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">30</span><span class="p">),</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">long</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">group</span><span class="p">),</span><span class="w"> 
               </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">coord_map</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Avg. Winter Temperature in Japan"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Data from Iowa Environment Mesonet 2016-2017"</span><span class="p">,</span><span class="w">
       </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_xkcd</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_color_gradient</span><span class="p">(</span><span class="n">low</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#08306B"</span><span class="p">)</span><span class="w">
</span>

<span class="c1"># Humidex: ####</span><span class="w">

</span><span class="n">climate_japan_map</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">30</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">lat</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">summer_humidex</span><span class="p">),</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_text_repel</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">City</span><span class="p">),</span><span class="w">
                  </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4.5</span><span class="p">,</span><span class="w">
                  </span><span class="n">max.iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">50000</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_polygon</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">JPN</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">30</span><span class="p">),</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">long</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">group</span><span class="p">),</span><span class="w"> 
               </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"black"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">coord_map</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Summer Humidex across Japan"</span><span class="p">,</span><span class="w">
       </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Data from Iowa Environment Mesonet 2016-2017"</span><span class="p">,</span><span class="w">
       </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">""</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_xkcd</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">14</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_color_gradient</span><span class="p">(</span><span class="n">low</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#FCBBA1"</span><span class="p">,</span><span class="w"> </span><span class="n">high</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"#67000D"</span><span class="p">)</span><span class="w">
</span>

A large area in the middle of Honshu Island that does not have any data is the Central Highland region, which is a very mountainous region and therefore there aren’t any airport weather stations located there for our plot. This is a great area to go if you love to ski and relax in an onsen! In Nagano Prefecture you can go to Jigokudani Monkey Park where you can see snow monkeys relaxing in the onsen!

Now for the Okinawa and Ryukyu Islands:

<span class="n">climate_japan_map</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">30</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">lon</span><span class="p">,</span><span class="w"> </span><span class="n">lat</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">color</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">winter_avg_temp</span><span class="p">),</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">3.5</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_text_repel</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">City</span><span class="p">),</span><span class="w">
                  </span><span class="n">family</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"xkcd"</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4.5</span><span class="p">,</span><span class="w">
                  </span><span class="n">max.iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">50000</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_polygon</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">JPN</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">filter</span><span class="p">(</span><span class="n">lat</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">30</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">long</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">133</span><span class="p">),</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">long</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lat</span><span class="p">,</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o&qu...

To leave a comment for the author, please follow the link and comment on their blog: R by R(yo).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)