<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/mapping-fall-foliage-with-sf/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/09/18/mapping-fall-foliage-with-sf/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I was socially engineered by <a rel="nofollow" href="http://twitter.com/yoniceedee" target="_blank">@yoniceedee</a> into creating today’s post due to being prodded with this tweet:</p>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Where to see the best fall foliage, based on your location: <a rel="nofollow" href="https://t.co/12pQU29ksB" target="_blank">https://t.co/12pQU29ksB</a> <a rel="nofollow" href="https://t.co/JiywYVpmno" target="_blank">pic.twitter.com/JiywYVpmno</a></p>
<p>— Vox (<a rel="nofollow" href="http://twitter.com/voxdotcom" target="_blank">@voxdotcom</a>) <a rel="nofollow" href="https://twitter.com/voxdotcom/status/909729977633050624" target="_blank">September 18, 2017</a></p></blockquote>
<p></p>
<p>Since there aren’t nearly enough <code>sf</code> and <code>geom_sf</code> examples out on the wild, wild #rstats web, here’s a short one that shows how to do basic <code>sf</code> operations, including how to plot <code>sf</code> objects in ggplot2 and animate a series of them with <code>magick</code>.</p>
<p>I’m hoping someone riffs off of this to make an interactive version with Shiny. If you do, definitely drop a link+note in the comments!</p>
<p>(If folks want some exposition here, let me know in the comments and I’ll rig up an R Markdown document with more step-by-step details.)</p>
<p>Full RStudio project file (with pre-cached data) is <a rel="nofollow" href="https://github.com/hrbrmstr/foliage" target="_blank">on GitHub</a>.</p>
<p><a rel="nofollow" href="https://i2.wp.com/rud.is/dl/foliage.gif?ssl=1" target="_blank"><img src="https://i2.wp.com/rud.is/dl/foliage.gif?resize=780%2C468&ssl=1" data-recalc-dims="1"></a></p>
<pre><code>library(rprojroot)
library(sf)
library(magick)
library(tidyverse) # NOTE: Needs github version of ggplot2

root &lt;- find_rstudio_root_file()

# &quot;borrow&quot; the files from SmokyMountains.com, but be nice and cache them to
# avoid hitting their web server for every iteration

c(&quot;https://smokymountains.com/wp-content/themes/smcom-2015/to-delete/js/us.json&quot;,
  &quot;https://smokymountains.com/wp-content/themes/smcom-2015/js/foliage2.tsv&quot;,
  &quot;https://smokymountains.com/wp-content/themes/smcom-2015/js/foliage-2017.csv&quot;) %>%
  walk(~{
    sav_tmp &lt;- file.path(root, &quot;data&quot;, basename(.x))
    if (!file.exists(sav_tmp)) download.file(.x, sav_tmp)
  })

# next, we read in the GeoJSON file twice. first, to get the counties
states_sf &lt;- read_sf(file.path(root, &quot;data&quot;, &quot;us.json&quot;), &quot;states&quot;, stringsAsFactors = FALSE)

# we only want the continental US
states_sf &lt;- filter(states_sf, !(id %in% c(&quot;2&quot;, &quot;15&quot;, &quot;72&quot;, &quot;78&quot;)))

# it doesn't have a CRS so we give it one
st_crs(states_sf) &lt;- 4326

# I ran into hiccups using coord_sf() to do this, so we convert it to Albers here
states_sf &lt;- st_transform(states_sf, 5070)


# next we read in the states
counties_sf &lt;- read_sf(file.path(root, &quot;data&quot;, &quot;us.json&quot;), &quot;counties&quot;, stringsAsFactors = FALSE)
st_crs(counties_sf) &lt;- 4326
counties_sf &lt;- st_transform(counties_sf, 5070)

# now, we read in the foliage data
foliage &lt;- read_tsv(file.path(root, &quot;data&quot;, &quot;foliage-2017.csv&quot;),
                    col_types = cols(.default=col_double(), id=col_character()))

# and, since we have a lovely sf tidy data frame, bind it together
left_join(counties_sf, foliage, &quot;id&quot;) %>%
  filter(!is.na(rate1)) -> foliage_sf

# now, we do some munging so we have better labels and so we can
# iterate over the weeks
gather(foliage_sf, week, value, -id, -geometry) %>%
  mutate(value = factor(value)) %>%
  filter(week != &quot;rate1&quot;) %>%
  mutate(week = factor(week,
                       levels=unique(week),
                       labels=format(seq(as.Date(&quot;2017-08-26&quot;),
                                         as.Date(&quot;2017-11-11&quot;), &quot;1 week&quot;),
                                     &quot;%b %d&quot;))) -> foliage_sf

# now we make a ggplot object for each week and save it out to a png
pb &lt;- progress_estimated(nlevels(foliage_sf$week))
walk(1:nlevels(foliage_sf$week), ~{

  pb$tick()$print()

  xdf &lt;- filter(foliage_sf, week == levels(week)[.x])

  ggplot() +
    geom_sf(data=xdf, aes(fill=value), size=0.05, color=&quot;#2b2b2b&quot;) +
    geom_sf(data=states_sf, color=&quot;white&quot;, size=0.125, fill=NA) +
    viridis::scale_fill_viridis(
      name=NULL,
      discrete = TRUE,
      labels=c(&quot;No Change&quot;, &quot;Minimal&quot;, &quot;Patchy&quot;, &quot;Partial&quot;, &quot;Near Peak&quot;, &quot;Peak&quot;, &quot;Past Peak&quot;),
      drop=FALSE
    ) +
    labs(title=sprintf(&quot;Foliage: %s &quot;, unique(xdf$week))) +
    ggthemes::theme_map() +
    theme(panel.grid=element_line(color=&quot;#00000000&quot;)) +
    theme(panel.grid.major=element_line(color=&quot;#00000000&quot;)) +
    theme(legend.position=&quot;right&quot;) -> gg

  ggsave(sprintf(&quot;%02d.png&quot;, .x), gg, width=5, height=3)

})

# we read them all back in and animate the foliage
sprintf(&quot;%02d.png&quot;, 1:nlevels(foliage_sf$week)) %>%
  map(image_read) %>%
  image_join() %>%
  image_animate(1)</code></pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/09/18/mapping-fall-foliage-with-sf/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/QmDkrqd2L8Q" height="1"></a></div>