Visualizing the Weather in Japan: A Sweltering Summer Story!

[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.

I’ve lived in a couple of countries where talking (or more typically whinging) about the weather is a national past time so I took things one step further, by making a blog post about it!

The past few months we’ve had an absolute hell of a summer here in Tokyo. It wasn’t just hot, it was extremely humid too making things doubly worse! As some kinda coping mechanism I’ve been using R to create cool visualizations throughout the summer. This post will be a place where I can show all of them along with the code. I will also cover methods of getting Japanese weather data using the jmastats (Japan Meteorological Agency) and riem (ASOS airport weather stations) packages.

Let’s Begin!

<span class="n">library</span><span class="p">(</span><span class="n">jpndistrict</span><span class="p">)</span><span class="w"> </span><span class="c1"># install GitHub version to get English names in jpnprefs</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">sf</span><span class="p">)</span><span class="w">          </span><span class="c1"># spatial polygons</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">     </span><span class="c1"># plotting essentials</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="c1"># data wrangling</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"># more data wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">purrr</span><span class="p">)</span><span class="w">       </span><span class="c1"># even more data wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">lubridate</span><span class="p">)</span><span class="w">   </span><span class="c1"># date wrangling</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rvest</span><span class="p">)</span><span class="w">       </span><span class="c1"># webscraping</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">polite</span><span class="p">)</span><span class="w">      </span><span class="c1"># responsible webscraping</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">stringr</span><span class="p">)</span><span class="w">     </span><span class="c1"># string wrangling</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="c1"># airport weather station data</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">jmastats</span><span class="p">)</span><span class="w">    </span><span class="c1"># Japan Meteorological Agency data</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="c1"># labels</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">gganimate</span><span class="p">)</span><span class="w">   </span><span class="c1"># animations</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">geofacet</span><span class="p">)</span><span class="w">    </span><span class="c1"># geographical facetting</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">gghighlight</span><span class="p">)</span><span class="w"> </span><span class="c1"># highlight stuff</span><span class="w">

</span><span class="c1"># font packages</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">loadfonts</span><span class="p">(</span><span class="n">device</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"win"</span><span class="p">)</span><span class="w">
</span>

Tokyo Average Temperature Heat map (1876-2016)

Back in July, at the height of the summer heatwave, I came across this visualization by Toyo Keizai News on Twitter. Interestingly they provided a .json file of the data so I wanted to try recreating this in R!

On one hand, we could just use the jmastats package that I mentioned earlier to grab the data ourselves but since I had never dealt with .JSON data before I felt it was as good time as any to finally dive into it. Also, why go through the trouble if you already have the data sitting right there for you?

Let’s go through what we have to do. First, use the read_json() function from the jsonlite package to read in the data. Then, set the names for each vector with the year it corresponds to. Oddly some of the temperature values are factors so we need to change them into numeric. Then using another mapper function, we assign a new year variable to each of the “temperature per year” data frames with their respective years. Basically we’re bringing the year label we created for each list into an explicit value in the single (combined) data frame that we map as the output.

<span class="n">tokyo_his_temp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">jsonlite</span><span class="o">::</span><span class="n">read_json</span><span class="p">(</span><span class="s2">"../data/temperature.json"</span><span class="p">,</span><span class="w"> </span><span class="n">simplifyVector</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">tokyo_weather_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tokyo_his_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">set_names</span><span class="p">(</span><span class="n">nm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1876</span><span class="o">:</span><span class="m">2018</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">map</span><span class="p">(</span><span class="o">~</span><span class="n">as.data.frame</span><span class="p">(</span><span class="n">.</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">modify_if</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="n">is.factor</span><span class="p">,</span><span class="w"> </span><span class="n">as.character</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
        </span><span class="n">modify_if</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="n">is.character</span><span class="p">,</span><span class="w"> </span><span class="n">as.numeric</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">map2_df</span><span class="p">(</span><span class="n">.</span><span class="p">,</span><span class="w"> </span><span class="nf">names</span><span class="p">(</span><span class="n">.</span><span class="p">),</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">.</span><span class="p">,</span><span class="w"> </span><span class="n">year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">.y</span><span class="p">))</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">avg_temp</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">mutate</span><span class="p">(</span><span class="n">year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">year</span><span class="p">))</span><span class="w">

</span><span class="n">tokyo_weather_df</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">glimpse</span><span class="p">()</span><span class="w">
</span>
## Observations: 17,371
## Variables: 2
## $ avg_temp <dbl> 17.0, 19.1, 17.9, 18.8, 19.1, 20.8, 16.7, 18.7, 16.8,...
## $ year     <dbl> 1876, 1876, 1876, 1876, 1876, 1876, 1876, 1876, 1876,...

Now we have a very lengthy data frame with the temperatures and their respective year. Now we have to add in the specific month-day pairs for each of the years. The heat map is going to be running from June 1st to September 30th, a span of 122 days. We can use the seq.Date() function to create a new variable of dates running from these two dates grouped by year.

<span class="n">tokyo_weather_df</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tokyo_weather_df</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">year</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="m">2018</span><span class="p">)</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">year</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="w">
    </span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">seq.Date</span><span class="p">(</span><span class="n">from</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as.Date</span><span class="p">(</span><span class="s2">"1876-06-01"</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">"day"</span><span class="p">,</span><span class="w">
                    </span><span class="n">length</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">122</span><span class="p">),</span><span class="w">
    </span><span class="n">date</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">format</span><span class="p">(</span><span class="n">date</span><span class="p">,</span><span class="w"> </span><span class="s2">"%m/%d"</span><span class="p">)</span><span class="w">
  </span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ungroup</span><span class="p">()</span><span class="w">

</span><span class="n">glimpse</span><span class="p">(</span><span class="n">tokyo_weather_df</span><span class="p">)</span><span class="w">
</span>
## Observations: 17,324
## Variables: 3
## $ avg_temp <dbl> 17.0, 19.1, 17.9, 18.8, 19.1, 20.8, 16.7, 18.7, 16.8,...
## $ year     <dbl> 1876, 1876, 1876, 1876, 1876, 1876, 1876, 1876, 1876,...
## $ date     <chr> "06/01", "06/02", "06/03", "06/04", "06/05", "06/06",...

Great! Now we have the data we need for plotting!

So, the point of this heat map visualization is to see the changes in temperature across time. We need to choose the right kind of color palette to emphasize the point we are trying to convey with this visualization. A great resource that I like to use is colorbrewer2.org where you can choose from a variety of classes and scales to fit your palette needs. The colors I chose came from the 8-class diverging palette, the website provides you with the Hex Code you need to pass into the appropriate scale_*() function. I also specify the breaks and labels for the color scale.

<span class="c1"># colorbrewer2.org: diverging 8-class palette</span><span class="w">
</span><span class="n">cols</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rev</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s1">'#d53e4f'</span><span class="p">,</span><span class="s1">'#f46d43'</span><span class="p">,</span><span class="s1">'#fdae61'</span><span class="p">,</span><span class="s1">'#fee08b'</span><span class="p">,</span><span class="s1">'#e6f598'</span><span class="p">,</span><span class="s1">'#abdda4'</span><span class="p">,</span><span class="s1">'#66c2a5'</span><span class="p">,</span><span class="s1">'#3288bd'</span><span class="p">))</span><span class="w">

</span><span class="n">labels</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">"10"</span><span class="p">,</span><span class="w"> </span><span class="s2">"12"</span><span class="p">,</span><span class="w"> </span><span class="s2">"14"</span><span class="p">,</span><span class="w"> </span><span class="s2">"16"</span><span class="p">,</span><span class="w"> </span><span class="s2">"18"</span><span class="p">,</span><span class="w"> </span><span class="s2">"20"</span><span class="p">,</span><span class="w"> </span><span class="s2">"22"</span><span class="p">,</span><span class="w"> </span><span class="s2">"24"</span><span class="p">,</span><span class="w"> </span><span class="s2">"26"</span><span class="p">,</span><span class="w"> </span><span class="s2">"28"</span><span class="p">,</span><span class="w"> </span><span class="s2">"30"</span><span class="p">,</span><span class="w"> </span><span class="s2">"32"</span><span class="p">)</span><span class="w">

</span><span class="n">breaks</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">seq</span><span class="p">(</span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">32</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="m">2</span><span class="p">))</span><span class="w">
</span>

The ggplot2 code I used was fairly straightforward. A nifty new package I started using lately is the glue package by Jim Hester, I used it here to format the text to appear in multiple lines in the plot (so I don’t have to use “” inside paste()). We’ll see more uses of this in my other visualizations later on!

<span class="n">tokyo_weather_df</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">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">date</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">year</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="n">avg_temp</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_tile</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_fill_gradientn</span><span class="p">(</span><span class="w">
    </span><span class="n">colours</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cols</span><span class="p">,</span><span class="w">
    </span><span class="n">labels</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">labels</span><span class="p">,</span><span class="w">
    </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">breaks</span><span class="p">,</span><span class="w">
    </span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">11.1</span><span class="p">,</span><span class="w"> </span><span class="m">33.2</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">guides</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">guide_colorbar</span><span class="p">(</span><span class="n">title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">expression</span><span class="p">(</span><span class="s2">"Temperature "</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">degree</span><span class="o">~</span><span class="n">C</span><span class="p">)),</span><span class="w">
                               </span><span class="n">reverse</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">title.position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"left"</span><span class="p">,</span><span class="w">
                               </span><span class="n">label.position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bottom"</span><span class="p">,</span><span class="w">
                               </span><span class="n">nrow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_reverse</span><span class="p">(</span><span class="n">limits</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">2017</span><span class="p">,</span><span class="w"> </span><span class="m">1876</span><span class="p">),</span><span class="w"> </span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">),</span><span class="w">
                  </span><span class="n">breaks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1876</span><span class="p">,</span><span class="w"> </span><span class="n">seq</span><span class="p">(</span><span class="m">1880</span><span class="p">,</span><span class="w"> </span><span class="m">2015</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="m">10</span><span class="p">),</span><span class="w"> </span><span class="m">2017</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_x_discrete</span><span class="p">(</span><span class="n">breaks</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">"06/01"</span><span class="p">,</span><span class="w"> </span><span class="s2">"07/01"</span><span class="p">,</span><span class="w"> </span><span class="s2">"08/01"</span><span class="p">,</span><span class="w"> </span><span class="s2">"09/01"</span><span class="p">,</span><span class="w"> </span><span class="s2">"09/30"</span><span class="p">),</span><span class="w">
                   </span><span class="n">labels</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">"June 1st"</span><span class="p">,</span><span class="w"> </span><span class="s2">"July 1st"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Aug. 1st"</span><span class="p">,</span><span class="w"> 
                              </span><span class="s2">"Sept. 1st"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Sept. 30th"</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">"Summers in Tokyo are Getting Longer and Hotter (1876-2017)"</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="n">glue</span><span class="o">::</span><span class="n">glue</span><span class="p">(</span><span class="s2">"
          One Row = One Year, From June 1st to September 30th
          Average Temperature (Celsius)
          "</span><span class="p">),</span><span class="w">
       </span><span class="n">caption</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Data from Toyo Keizai News via Japan Meteorological Agency"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_minimal</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">"Roboto Condensed"</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">12</span><span class="p">),</span><span class="w">
        </span><span class="n">axis.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">panel.grid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">legend.position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bottom"</span><span class="p">,</span><span class="w">
        </span><span class="n">legend.key.width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">unit</span><span class="p">(</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="s2">"cm"</span><span class="p">),</span><span class="w">
        </span><span class="n">plot.margin</span><span class="o">=</span><span class="n">unit</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">1</span><span class="p">,</span><span class="m">1.5</span><span class="p">,</span><span class="m">1.2</span><span class="p">),</span><span class="s2">"cm"</span><span class="p">))</span><span class="w">
</span>

Although Toyo Keizai stated that the data came from the Japan Meteorological Agency (I’ll mostly be referring to them as the JMA from here on out) and the measure is the average temperature for that day, I don’t know if the temperatures comes from the entirety of the Tokyo Prefecture (including the islands off the coast) or just from the 23 special wards that make up Tokyo City. It’s not necessarily a huge concern but it is something to consider.

For those that want to take a closer look I also created a Shiny app version of the above (here) so you can scroll around and look at the temperature for a specific day/year.

Tokyo Climate Stripes

Back in August I came across Ed Hawkins’ cool climate strip visualization, again on Twitter. There were several versions out for different countries and cities but I didn’t see Tokyo on there, so thinking it’ll be a good opportunity for some practice I went ahead and tried to recreate it!

This time I’m getting data straight from the JMA, the “annual temperature table” from this link here.

<span class="n">url</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="s2">"http://www.data.jma.go.jp/obd/stats/etrn/view/annually_s.php?prec_no=44&block_no=47662"</span><span class="w">

</span><span class="n">session_jma</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_nodes</span><span class="p">(</span><span class="s2">"#tablefix1"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">.</span><span class="p">[[</span><span class="m">1</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="n">fill</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">header</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>

Welp. What a giant mess of a table! Thankfully our trust dplyr verbs and regex can solve our problems. Et Voilà!

<span class="n">tokyo_year_avg_temp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">session_jma</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">year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="n">avg_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="n">avg_high</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="n">avg_low</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">10</span><span class="p">,</span><span class="w"> 
         </span><span class="n">high_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">11</span><span class="p">,</span><span class="w"> </span><span class="n">low_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="m">12</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">slice</span><span class="p">(</span><span class="o">-</span><span class="nf">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">))</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">avg_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">avg_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="s2">"\\]"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w">
         </span><span class="n">avg_high</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">avg_high</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="s2">"\\]"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w">
         </span><span class="n">avg_low</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">avg_low</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="s2">"\\]"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w">
         </span><span class="n">high_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">high_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="s2">"\\]"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w">
         </span><span class="n">low_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">low_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">str_remove</span><span class="p">(</span><span class="s2">"\\]"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(),</span><span class="w">
         </span><span class="n">year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">as_factor</span><span class="p">(</span><span class="n">year</span><span class="p">))</span><span class="w">

</span><span class="n">tokyo_year_avg_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">glimpse</span><span class="p">()</span><span class="w">
</span>
## Observations: 144
## Variables: 6
## $ year      <fct> 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883...
## $ avg_temp  <dbl> 17.0, 13.6, 14.2, 13.8, 14.6, 14.1, 13.8, 14.0, 13.3...
## $ avg_high  <dbl> 21.9, 18.7, 19.1, 18.3, 19.4, 19.2, 18.8, 18.6, 17.8...
## $ avg_low   <dbl> 12.2, 8.3, 8.9, 9.6, 10.0, 9.1, 8.5, 9.0, 9.1, 8.6, ...
## $ high_temp <dbl> 35.1, 35.6, 34.9, 35.1, 33.9, 33.2, 34.2, 34.2, 32.8...
## $ low_temp  <dbl> -3.6, -9.2, -4.8, -7.6, -5.5, -6.8, -8.4, -6.3, -7.8...

I was wondering what kind of color palette Ed used… so going back to the Twitter thread I saw somebody opine that he might have used the “10-class divergent RD-BU” from colorbrewer2.org. I checked it out and it seemed very similar so I went ahead with that!

<span class="c1"># 10 Class Divergent Rd-Bu: http://colorbrewer2.org/#type=diverging&scheme=RdBu&n=10</span><span class="w">
</span><span class="n">temp_cols</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rev</span><span class="p">(</span><span class="nf">c</span><span class="p">(</span><span class="s1">'#67001f'</span><span class="p">,</span><span class="s1">'#b2182b'</span><span class="p">,</span><span class="s1">'#d6604d'</span><span class="p">,</span><span class="s1">'#f4a582'</span><span class="p">,</span><span class="s1">'#fddbc7'</span><span class="p">,</span><span class="w">
               </span><span class="s1">'#d1e5f0'</span><span class="p">,</span><span class="s1">'#92c5de'</span><span class="p">,</span><span class="s1">'#4393c3'</span><span class="p">,</span><span class="s1">'#2166ac'</span><span class="p">,</span><span class="s1">'#053061'</span><span class="p">))</span><span class="w">
</span>

The plot was very simple. I used geom_bar() for the stripes and specified width = 1 so that the bars left no gap in between each other. Then I erased all the axes and labels with theme_void().

<span class="n">tokyo_year_avg_temp</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="c1"># filter out 1875 and 2018 due to uncertainties in the measurements</span><span class="w">
  </span><span class="c1"># http://www.data.jma.go.jp/obd/stats/data/mdrr/man/remark.html</span><span class="w">
  </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">year</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">1875</span><span class="p">,</span><span class="w"> </span><span class="m">2018</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">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">year</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="n">avg_temp</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_bar</span><span class="p">(</span><span class="n">position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"fill"</span><span class="p">,</span><span class="w"> </span><span class="n">width</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_y_continuous</span><span class="p">(</span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">0.01</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_x_discrete</span><span class="p">(</span><span class="n">expand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">0</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">scale_fill_gradientn</span><span class="p">(</span><span class="n">colors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp_cols</span><span class="p">,</span><span class="w"> </span><span class="s2">"Average Temperature (Celsius)"</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">"Tokyo: Annual Average Temperature (1876-2017)"</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">"One Stripe = One Year, Left: 1876, Right: 2017"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme_void</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">"Roboto Condensed"</span><span class="p">),</span><span class="w">
        </span><span class="n">legend.position</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"bottom"</span><span class="p">,</span><span class="w">
        </span><span class="n">legend.title</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">"Roboto Condensed"</span><span class="p">))</span><span class="w">
</span>

Very minimal yet very effective at showing the shift in temperature over time!

Gathering Japanese Weather Data ft. riem & jmastats

The two places I know where to get Japan weather data using R is via riem and jmastats. In this section I’ll go through what you need to do to get the data from both of these packages.

riem

The riem package is a ROpenSci project authored by Maëlle Salmon that lets you download airport weather data from the Iowa Environmental Mesonet website. You can search for airport networks from all over the world and because the Japan ASOS weather stations take measurements at 30 minute intervals, you can get A LOT of data!

I found about this package last year through a #rstats mini event on Twitter (started off here) where people started to create XKCD-themed charts showing the most comfortable (weather-wise) place to live. I even made a blog post last year showing a version of this chart for Japan!

In the example below I’ll just be using the same set of Japanese airports I used back then, the top 50 busiest airports in Japan. I feel like it’s a fair representation across the prefectures and although I could use the “list of airports in Japan” wiki page that has a lot of small airports and military base airports that wouldn’t really be appropriate.

A few minor changes were necessary however as oddly I can’t seem to get the data from any of the Nagoya airports through this package nor the API on the Iowa Environmental Mesonet website…they used to work when I did the XKCD blog post a year ago but whatever, we’ll skip over those for now. We’ll just use RJGG which is the Chubu Centrair Int’l Airport as it’s the official Nagoya (and the Chubu region’s) international airport nowadays anyways…

<span class="c1"># web scrape busiest Japan airport codes</span><span class="w">
</span><span class="n">session</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">bow</span><span class="p">(</span><span class="s2">"https://en.wikipedia.org/wiki/List_of_the_busiest_airports_in_Japan"</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">scrape</span><span class="p">(</span><span class="n">session</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">html_nodes</span><span class="p">(</span><span class="s2">"table.wikitable:nth-child(8)"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">.</span><span class="p">[[</span><span class="m">1</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">japan_airports_clean</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">janitor</span><span class="o">::</span><span class="n">clean_names</span><span class="p">()</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">city</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cityserved</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">iconv</span><span class="p">(</span><span class="n">from</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"UTF-8"</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"ASCII//TRANSLIT"</span><span class="p">))</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">iata_icao</span><span class="p">)</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="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</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">case_when</span><span class="p">(</span><span class="w">
    </span><span class="n">ICAO</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"RJAH"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Ibaraki"</span><span class="p">,</span><span class="w">            </span><span class="c1"># fix Ibaraki Airport listed as Tokyo</span><span class="w">
    </span><span class="n">ICAO</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"RJOK"</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="s2">"Kochi"</span><span class="p">,</span><span class="w">              </span><span class="c1"># fix Kochi, Kochi to Kochi</span><span class="w">
    </span><span class="kc">TRUE</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">city</span><span class="w">
  </span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="c1"># filter out Narita and Kansai Airports</span><span class="w">
  </span><span class="c1"># filter out RJNA (Nagoya) as not working for some reason</span><span class="w">
  </span><span class="c1"># RJGG is also Nagoya so I'll use that instead</span><span class="w">
  </span><span class="n">filter</span><span class="p">(</span><span class="o">!</span><span class="n">ICAO</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="s2">"RJAA"</span><span class="p">,</span><span class="w"> </span><span class="s2">"RJBB"</span><span class="p">,</span><span class="w"> </span><span class="s2">"RJNA"</span><span class="p">))</span><span class="w">
</span>

Now that we have the ICAO codes for the Japanese airports we want, we can pass these codes into the riem_measures() function along with the date range. We use a mapper function map_df() as we want to iterate over all the airport codes in one go.

<span class="c1"># grab weather data from stations</span><span class="w">
</span><span class="n">summer_weather_riem_raw</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">japan_airports_clean</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">"2018-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">"2018-08-31"</span><span class="p">)</span><span class="w">
</span>

Great! Now we can do a bit of cleaning and also calculate the daily averages for a few temperature related measures as what riem_measures() gives us is a bit too granular for our purposes. Then, combine it back with the airport metadata.

<span class="c1"># calculate daily averages </span><span class="w">
</span><span class="n">sum_air</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">summer_weather_riem_raw</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">time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">as_date</span><span class="p">(</span><span class="n">valid</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> </span><span class="n">ymd</span><span class="p">())</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">time</span><span class="p">,</span><span class="w"> </span><span class="n">into</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">"year"</span><span class="p">,</span><span class="w"> </span><span class="s2">"month"</span><span class="p">,</span><span class="w"> </span><span class="s2">"day"</span><span class="p">),</span><span class="w"> </span><span class="n">sep</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">group_by</span><span class="p">(</span><span class="n">month</span><span class="p">,</span><span class="w"> </span><span class="n">day</span><span class="p">,</span><span class="w"> </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">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">tmpf</span><span class="p">),</span><span class="w">
            </span><span class="n">max_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">max</span><span class="p">(</span><span class="n">tmpf</span><span class="p">),</span><span class="w">
            </span><span class="n">min_temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">min</span><span class="p">(</span><span class="n">tmpf</span><span class="p">),</span><span class="w">
            </span><span class="n">avg_dewp</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">dwpf</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">ungroup</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="c1"># join riem data with airport data</span><span class="w">
  </span><span class="n">left_join</span><span class="p">(</span><span class="n">japan_airports_clean</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="o">%>%</span><span class="w"> 
  </span><span class="n">glimpse</span><span class="p">()</span><span class="w">
</span>
## Observations: 4,172
## Variables: 10
## $ month    <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06",...
## $ day      <chr> "01", "01", "01", "01", "01", "01", "01", "01", "01",...
## $ station  <chr> "RJAH", "RJBE", "RJCB", "RJCC", "RJCH", "RJCK", "RJCM...
## $ avg_temp <dbl> 66.60645, 69.80000, 55.40000, 57.82449, 59.69231, 57....
## $ max_temp <dbl> 75.2, 73.4, 59.0, 69.8, 64.4, 64.4, 57.2, 73.4, 75.2,...
## $ min_temp <dbl> 59.0, 64.4, 50.0, 51.8, 55.4, 50.0, 44.6, 64.4, 68.0,...
## $ avg_dewp <dbl> 56.96774, 57.68000, 48.07143, 50.62449, 56.23077, 43....
## $ airport  <chr> "Ibaraki Airport", "Kobe Airport", "Obihiro Airport",...
## $ city     <chr> "Ibaraki", "Kobe", "Obihiro", "Sapporo", "Hakodate", ...
## $ IATA     <chr> "IBR", "UKB", "OBO", "CTS", "HKD", "KUH", "MMB", "UBJ...

With that done we can play around with the data a bit like changing from Fahrenheit to Celsius with the help of the weathermetrics package.

<span class="n">library</span><span class="p">(</span><span class="n">weathermetrics</span><span class="p">)</span><span class="w">
</span><span class="c1"># convert from Fahrenheit to Celsius!</span><span class="w">
</span><span class="n">sum_air</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_at</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">contains</span><span class="p">(</span><span class="s2">"temp"</span><span class="p">)),</span><span class="w"> 
            </span><span class="o">~</span><span class="n">convert_temperature</span><span class="p">(</span><span class="n">temperature</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">.</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="o">%>%</span><span class="w"> 
  </span><span class="n">select</span><span class="p">(</span><span class="n">contains</span><span class="p">(</span><span class="s2">"temp"</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">glimpse</span><span class="p">()</span><span class="w">
</span>
## Observations: 4,172
## Variables: 3
## $ avg_temp <dbl> 19.23, 21.00, 13.00, 14.35, 15.38, 14.20, 11.71, 21.5...
## $ max_temp <dbl> 24, 23, 15, 21, 18, 18, 14, 23, 24, 20, 26, 26, 24, 2...
## $ min_temp <dbl> 15, 18, 10, 11, 13, 10, 7, 18, 20, 13, 17, 14, 16, 18...

You can also calculate the humidex (index of comfort combining temperature and humidity) with the comf package:

<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">sum_air</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate_at</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">contains</span><span class="p">(</span><span class="s2">"avg"</span><span class="p">)),</span><span class="w"> 
            </span><span class="o">~</span><span class="n">convert_temperature</span><span class="p">(</span><span class="n">temperature</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">.</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="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">avg_temp</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">avg_dewp</span><span class="p">))</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">month</span><span class="p">,</span><span class="w"> </span><span class="n">day</span><span class="p">,</span><span class="w"> </span><span class="n">station</span><span class="p">,</span><span class="w"> </span><span class="n">airport</span><span class="p">,</span><span class="w"> </span><span class="n">humidex</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">slice</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">5</span><span class="p">)</span><span class="w">
</span>
## # A tibble: 5 x 5
##   month day   station airport             humidex
##   <chr> <chr> <chr>   <chr>                 <dbl>
## 1 06    01    RJAH    Ibaraki Airport       15.4 
## 2 06    01    RJBE    Kobe Airport          17.4 
## 3 06    01    RJCB    Obihiro Airport        8.22
## 4 06    01    RJCC    New Chitose Airport    9.76
## 5 06    01    RJCH    Hakodate Airport      11.2

I took out a lot of the other weather variables like wind direction, wind speed, visibility, pressure altimeter, etc. that you can get from riem as I was only interested in the temperature stuff but you can see what each of the variables are from the documentation or you can go directly to the Iowa Environmental Mesonet website to find more details!

jmastats

In the visualizations I made in the first section, I used data from the JMA but it was either indirectly or through web scraping. So now I’ll show you a way to get data directly using R with the jmastats package! This useful package is authored by Shinya Uryu and it is still in the early stages of its development. Through a variety of presentations and gists (Ex. 1, 2) he has demonstrated some of the cool things you can do with it, even if you can’t read Japanese I suggest you check it out for the visualizations!

First, let’s create a small map of the JMA weather stations in Tokyo to give us a little context about the Japan Meteorological Agency and its weather stations. We can use the weather stations metadata from the jmastats package and filter for stations located in Tokyo Prefecture to create our map.

The Automated Meteorological Data Acquisition System (AMeDAS) is the 1,300 station network spread throughout Japan that uses automatic observation equipment to measure and record data such as the weather, wind direction/speed, precipitation, humidity and more. There are both manned and unmanned stations which send data back to the JMA Headquarters at 10 second or 10 minutes intervals depending on the type of data.

Here’s a picture of one of these AMeDAS installations:

I’ll also include the regional weather headquarters in Tokyo for this map (this station doesn’t use AMeDAS). Below I pull out the latitude and longitude coordinates from the geometry column of the stations data set.

<span class="n">tky_stations_raw</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">jmastats</span><span class="o">::</span><span class="n">stations</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">area</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"東京"</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">address</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">tky_stations</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">tky_stations_raw</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">mutate</span><span class="p">(</span><span class="w">
    </span><span class="n">centroid</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">map</span><span class="p">(</span><span class="n">geometry</span><span class="p">,</span><span class="w"> </span><span class="n">st_centroid</span><span class="p">),</span><span class="w">
    </span><span class="n">coords</span><span class="w"> </span><span class="o">=</span><span class="w"...

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)