<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/sakura-blossoms-in-japan/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://opiateforthemass.es/articles/sakura/"> Opiate for the masses</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h1>Springtime and cherry blossoms</h1>
<p><span>I</span>t is spring, and the flowers bloom. One tree in particular symbolises this: the Japanese cherry (sakura) tree. Every year, millions of Japanese enjoy <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Hanami"><em>hanami</em></a>, jointly watching the cherry blossoms, philosophising about the the transient nature of beauty, and the inevitability of death.</p>
<p>There are regular “sakura forecasts” on TV, and elsewhere. The Japanese National Tourist Office has its own map and details <a target="_blank" rel="nofollow" href="https://www.jnto.go.jp/sakura/eng/index.php">available on their website</a> as well.</p>
<p>The data and graph section of The Economist recently published a beautiful chart showing <a target="_blank" rel="nofollow" href="https://www.economist.com/blogs/graphicdetail/2017/04/elephant-bloom">1200 years of data on the cherry blossom</a> – as well as one of their customary great puns: “the elephant in the bloom”. I wondered if I could replicate the graph in R.</p>
<p>I remembered an emoji font I had stumbled across, and that there is a sakura (cherry blossom) emoji. I also felt a bit competitive – with a <a target="_blank" rel="nofollow" href="http://opiateforthemass.es/authors/#yuki">Japanese</a> on this blog’s author roll, and <a target="_blank" rel="nofollow" href="http://opiateforthemass.es/authors/#jess">another author</a> being a <a target="_blank" rel="nofollow" href="http://opiateforthemass.es/articles/emoticons-in-R/">certified</a> <a target="_blank" rel="nofollow" href="http://opiateforthemass.es/articles/emoji-analysis/">expert</a> on emojis, I felt that I needed show off that I, too, can poach in their areas of expertise!</p>
<h1>Data</h1>
<p>The Economist’s article mentions a professor Yasuyuki Aono, who wrote several papers using data on Japanese cherry blossom dates. A quick googleing brought up his <a target="_blank" rel="nofollow" href="http://atmenv.envi.osakafu-u.ac.jp/aono/aono-e/">homepage</a>. He also linked to a page <a target="_blank" rel="nofollow" href="http://atmenv.envi.osakafu-u.ac.jp/aono/kyophenotemp4/">detailing the cherry blossom data</a>. Fantastic! A pretty straightforward data grab, it is then.</p>
<figure>
<pre><code data-lang="r"><span>if</span><span>(</span><span>!</span><span>file.exists</span><span>(</span><span>&quot;sakura.xls&quot;</span><span>)){</span><span>
</span><span># data is available on AONO's university homepage: </span><span> </span><span># http://atmenv.envi.osakafu-u.ac.jp/aono/kyophenotemp4/
</span><span> </span><span>aono.dta</span><span> </span><span>&lt;-</span><span> </span><span>&quot;http://atmenv.envi.osakafu-u.ac.jp/osakafu-content/uploads/sites/251/2015/10/KyotoFullFlower7.xls&quot;</span><span>
</span><span># make sure to read as binary, because windows is stupid
</span><span> </span><span>download.file</span><span>(</span><span>aono.dta</span><span>,</span><span> </span><span>&quot;sakura.xls&quot;</span><span>,</span><span> </span><span>mode</span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>}</span><span> </span><span># read in data
</span><span>dta</span><span> </span><span>&lt;-</span><span> </span><span>readxl</span><span>::</span><span>read_excel</span><span>(</span><span>&quot;sakura.xls&quot;</span><span>,</span><span> </span><span>skip</span><span> </span><span>=</span><span> </span><span>25</span><span>)</span><span> </span><span>%>%</span><span> </span><span># make &quot;good&quot; column names
</span><span> </span><span>setNames</span><span>(</span><span>make.names</span><span>(</span><span>names</span><span>(</span><span>.</span><span>),</span><span> </span><span>unique</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span> </span><span>mutate</span><span>(</span><span>
</span><span># convert the blossom date to R dateformat </span><span> </span><span>blossom.date</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>paste0</span><span>(</span><span>sprintf</span><span>(</span><span>&quot;%04d&quot;</span><span>,</span><span> </span><span>AD</span><span>),</span><span> </span><span>&quot;0&quot;</span><span>,</span><span> </span><span>Full.flowering.date</span><span>),</span><span> </span><span>format</span><span> </span><span>=</span><span> </span><span>&quot;%Y%m%d&quot;</span><span>)</span><span>
</span><span>)</span></code></pre>
</figure>
<p>We download the data if not available on disk, do some cleaning of column names, and generate a proper date column of the full flowering date, since this is the data with the most observations.</p>
<p>It’s a fascinating dataset: 1200 years of human records on a yearly event! It’s not often that one can work with these kinds of data. For this, I would like to thank professor Aono. Generating such a historical dataset is a tremendous amount of work (he probably had to find and read through hundreds of historical records), and providing the data on his homepage is not something I see often in academia.</p>
<h1>Graphing</h1>
<p>The original emoji graphing system I had in mind, <a target="_blank" rel="nofollow" href="https://github.com/dill/emoGG"><code>emoGG</code></a>, worked, but I could not change the size of the individual emojis. It turns out, <code>emoGG</code> is deprecated, and I should look at <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/emojifont/vignettes/emojifont.html"><code>emojifont</code></a> instead.</p>
<p><code>emojifont</code> uses <code>geom_text()</code> to draw the emojis onto a <code>ggplot()</code> graph. After a quick install of the font I was ready to go. Two things one needs to keep in mind when working with <code>emojifont</code>: you will need to load the font to start with, and if you’re using RStudio, you will need to initialise a new graphics environment. The standard RStudio environment is not able to preview/show the fonts. Both can be accomplished with these commands:</p>
<figure>
<pre><code data-lang="r"><span># need this to load the font
</span><span>load.emojifont</span><span>()</span><span>
</span><span># only needed for RStudio
</span><span>windows</span><span>()</span></code></pre>
</figure>
<p>One other thing that was problematic: <code>emojifont</code> uses a font to populate the graph by <code>ggplot2::aes(label = &quot;labelname&quot;)</code> to specify the emoji. It needs the label pulled <em>from the original dataset</em>, and not generated within the <code>aes()</code>.</p>
<figure>
<pre><code data-lang="r"><span># plot as sakuras
</span><span>dta</span><span> </span><span>%>%</span><span> </span><span># include the sakura as emoji character in dataset
</span><span> </span><span>mutate</span><span>(</span><span>
</span><span>sakura.emoji</span><span> </span><span>=</span><span> </span><span>emoji</span><span>(</span><span>&quot;cherry_blossom&quot;</span><span>),</span><span>
</span><span># join to a common year for axis label (2000 is a leap year)
</span><span> </span><span>common.date</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>paste0</span><span>(</span><span>&quot;2000-&quot;</span><span>,</span><span> </span><span>format</span><span>(</span><span>blossom.date</span><span>,</span><span> </span><span>&quot;%m-%d&quot;</span><span>)),</span><span> </span><span>&quot;%Y-%m-%d&quot;</span><span>)</span><span>
</span><span>)</span><span> </span></code></pre>
</figure>
<p>I created the <code>common.date</code> variable to scale the y-axis of the graph to a month-day format.</p>
<figure>
<pre><code data-lang="r"><span>dta</span><span> </span><span>%>%</span><span> </span><span># plot in ggplot
</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span>=</span><span>AD</span><span>,</span><span> </span><span>y</span><span>=</span><span>common.date</span><span>))</span><span>+</span><span>
</span><span># alternatively, with geom_emoji:
</span><span> </span><span># geom_emoji(&quot;cherry_blossom&quot;, x=dta$AD, y=dta$Full.flowering.date..DOY., size = 3)+
</span><span> </span><span>#geom_point(size = 0.5, colour = &quot;red&quot;)+
</span><span> </span><span># include emoji as text, h-/vjust to center; strange it needs vjust 0.25 -- why? 975572 BD77A4
</span><span> </span><span>geom_text</span><span>(</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>sakura.emoji</span><span>,</span><span> </span><span>hjust</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0.25</span><span>),</span><span> </span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;EmojiOne&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;#FF506E&quot;</span><span>)</span><span>+</span><span>
</span><span># trend line
</span><span> </span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;loess&quot;</span><span>,</span><span> </span><span>span</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;#D2A5C2&quot;</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span></code></pre>
</figure>
<p>To copy the Economist’s graph as closely as possible, I manually set the y-axis breaks. The x-axis breaks are more interesting, because the original graphs has major ticks every 200 years, labelled, and minor ticks every 100 years in between, which are not labelled. This is unfortunately not possible in <code>ggplot</code>, currently, so I had to resort to a workaround.</p>
<p>I create the sequence of years, with ticks every 100 years (so <code>800, 900, 1000, ...</code>). I then “overwrite” every even element (<code>900, 1100, 1300, ...</code>) of the vector, resulting in a “blank” label for those years. The axis label would thus look like this: <code>800, &quot;&quot;, 1000, &quot;&quot;, 1200, ...</code>.</p>
<figure>
<pre><code data-lang="r"><span>scale_y_date</span><span>(</span><span>
</span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Date of cherry-blossom peak-bloom&quot;</span><span>,</span><span>
</span><span>breaks</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;2000-03-27&quot;</span><span>,</span><span> </span><span>&quot;2000-04-01&quot;</span><span>,</span><span> </span><span>&quot;2000-04-10&quot;</span><span>,</span><span> </span><span>&quot;2000-04-20&quot;</span><span>,</span><span> </span><span>&quot;2000-05-01&quot;</span><span>,</span><span> </span><span>&quot;2000-05-04&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>as.Date</span><span>(),</span><span>
</span><span># Apr-01
</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>scales</span><span>::</span><span>date_format</span><span>(</span><span>&quot;%b-%d&quot;</span><span>)</span><span>
</span><span>)</span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>
</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>800</span><span>,</span><span> </span><span>2020</span><span>),</span><span>
</span><span># axis ticks every 200 years
</span><span> </span><span>breaks</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>800</span><span>,</span><span> </span><span>2000</span><span>,</span><span> </span><span>100</span><span>),</span><span>
</span><span># no minor axis ticks in ggplot2::theme(): http://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels
</span><span> </span><span># length(breaks): 13; replace every even element with empty string, to remove from axis labels
</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>replace</span><span>(</span><span>seq</span><span>(</span><span>800</span><span>,</span><span> </span><span>2000</span><span>,</span><span> </span><span>100</span><span>),</span><span> </span><span>seq</span><span>(</span><span>2</span><span>,</span><span> </span><span>12</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;&quot;</span><span>),</span><span> </span><span>name</span><span> </span><span>=</span><span> </span><span>&quot;Year&quot;</span><span>
</span><span>)</span></code></pre>
</figure>
<p>All that remains then are some <code>theme()</code> hacking to get the graph pretty.</p>
<p>Behold, 1200 years of cherry blossom history!</p>
<p><img alt="sakura plot" src="https://i1.wp.com/opiateforthemass.es/images/safferli/sakura.png?w=456" data-recalc-dims="1"></p>
<p>I’m not super happy with the graph colours, it seems too “light”, and pastel. Not really very readable. The Economist’s version is in purple; much more readable and much prettier to look at. I did want to use the sakura pink tones, though, and thus the graph colour scheme seems as fragile and transient as the actual sakura blossoms.</p>
<h1>Concluding remarks</h1>
<p>The code is available on <a target="_blank" rel="nofollow" href="https://github.com/safferli/sakura">github</a>, as always.</p>
<p>Aono uses the data to predict March temperatures, and in <a target="_blank" rel="nofollow" href="http://link.springer.com/article/10.1007%2Fs00484-014-0854-0">various</a> <a target="_blank" rel="nofollow" href="http://link.springer.com/article/10.1007%2Fs00484-009-0272-x">papers</a> he <a target="_blank" rel="nofollow" href="http://onlinelibrary.wiley.com/doi/10.1002/joc.1594/abstract">manages</a> to do this to an astonishing degree of accuracy: 0.1°C.</p>
<p>It’s very disheartening to see the blossom dates dramatically moving earlier into the year in the last 50 years. Global warming is to blame, and it always saddens me that people chose to ignore these facts… As a cold-loving northern German, I will need to find a retirement location in northern Norway, if things continue the way they do!</p>
<p><a target="_blank" rel="nofollow" href="http://opiateforthemass.es/articles/sakura/">Sakura blossoms in Japan</a> was originally published by Kirill Pomogajko at <a target="_blank" rel="nofollow" href="http://opiateforthemass.es/">Opiate for the masses</a> on April 11, 2017.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://opiateforthemass.es/articles/sakura/"> Opiate for the masses</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/9C9Css2DxAM"></a></div>