<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/mapping-unemployment-data-2016/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/map-unemployment-nov-2016/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The last few posts at Sharp Sight about data analysis <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-visualization/">have been long</a> and <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-wrangling/">fairly intense</a>.</p>
<p>Let’s do something a little more fun. Let’s make a quick map.</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1484px) 100vw, 1484px" alt="" class="wp-image-3030" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/unemployment_US_choropleth-map_2016-nov-1.png?resize=456%2C286" data-recalc-dims="1"><br>
</a></p>
<h1>How to make a compelling map,<br>in a few dozen lines of code</h1>
<p>The code to create this map is surprisingly brief. </p>
<pre class="wp-code-highlight">
#==============
# LOAD PACKAGES
#==============

library(ggplot2)
library(viridis)

#==========
# LOAD DATA
#==========

url.unemploy_map &lt;- url(&quot;http://sharpsightlabs.com/wp-content/datasets/unemployment_map_data_2016_nov.RData&quot;)

load(url.unemploy_map)

#===========
# CREATE MAP
#===========

ggplot() +
  geom_polygon(data = map.county_unemp, aes(x = long, y = lat, group = group, fill = unemployed_rate)) +
  geom_polygon(data = map.states, aes(x = long, y = lat, group = group), color = &quot;#EEEEEE&quot;, fill = NA, size = .3) +
  coord_map(&quot;albers&quot;, lat0 = 30, lat1 = 40) +
  labs(title = &quot;United States unemployment rate, by county&quot; , subtitle = &quot;November, 2016&quot;) +
  labs(fill = &quot;% unemployed&quot;) +
  scale_fill_viridis() +
  theme(text = element_text(family = &quot;Gill Sans&quot;, color = &quot;#444444&quot;)
        ,plot.title = element_text(size = 30)
        ,plot.subtitle = element_text(size = 20)
        ,axis.text = element_blank()
        ,axis.title = element_blank()
        ,axis.ticks = element_blank()
        ,panel.grid = element_blank()
        ,legend.position = c(.9,.4)
        ,legend.title = element_text(size = 16)
        ,legend.background = element_blank()
        ,panel.background = element_blank()
        )


</pre>
<p>Now, I will admit that there was a fair amount of data manipulation that made this map possible. I’ve provided the dataset to you (you can access it and use it immediately), so you don’t have to do the hard work of <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-wrangling/">gathering, wrangling, and shaping</a> this data in order to create this map.</p>
<p>Nevertheless, once you have the dataset ready, the final code to create the map is only about 20 lines. Once again (and for the record) this is why I love .  gives you the tools to create compelling data visualizations with relative ease and simplicity.</p>
<p>A few other things to note:</p>
<h2>This is a cousin of the heatmap</h2>
<p>This type of map is called a choropleth map. The chloropleth is sort of a cousin of the heatmap. The primary difference is that to <a href="http://sharpsightlabs.com/blog/heatmap-ggplot2-simple/">create the heatmap in </a>, we use , whereas to create this choropleth map, we use . In either case though, we’re plotting shapes, and shading those shapes in proportion to some metric. Syntactically, in , we shade those regions by mapping a variable to the  aesthetic.</p>
<p>Having said that, there are clear differences as well. Getting the polygon data for the counties and manipulating the data into shape is much harder for a choropleth than for a typical heatmap. Getting data for heatmaps is typically much easier.</p>
<p>Having said that, I recommend that if you want to learn to make a map like this, <a href="http://sharpsightlabs.com/blog/heatmap-ggplot2-simple/">start with the heatmap first</a>. Once again, I’m recommending that you learn the basics first, because basic techniques serve as a foundation for more complicated ones.</p>
<p>If you start with the heatmap, you’ll get some practice with a few critical skills. Namely, you’ll be able to practice working with different color palettes. To create compelling heatmaps (and choropleths), you’ll need to know how to use colors to create the right visual effect. </p>
<p>Learning the heatmap will also give you  syntax practice, and show you how to map variables to the  aesthetic. </p>
<h2>We built this plot in layers</h2>
<p>In many of my recent blog posts, I’ve emphasized “layering” as an <a href="http://sharpsightlabs.com/blog/r-package-think-about-visualization/">essential principle of data visualization</a>.</p>
<p>In this visualization, it’s subtle, but the layering principle is still at work. </p>
<p>There are two primary layers here. Go through the code and see if you can identify them.</p>
<p>Next, as an exercise, try removing the “state” layer and see what happens. Why is it useful to have? Also, notice that the states are only outlines (i.e., they aren’t filled in …). Why did I do that, and how did I accomplish it? Leave your answers to these in the comments below …</p>
<h2>Maps aren’t good for making precise comparisons</h2>
<p>I’ll admit that I quite like maps. As a data scientist, they’re fun to make. They’re compelling and beautiful to look at (if you execute them well).</p>
<p>But a map like this has limitations. </p>
<p>The big limitation is that when data is encoded as color (as we’ve done in this map) humans aren’t good at making precise distinctions between values. For example, if you live in the US, try identifying your home county. Can you tell the exact unemployment rate? Probably not. Moreover, try to compare one county vs another. You’ll quickly realize that you can make general statements like “county X has higher unemployment than county Y,” but you won’t be able to make <em>precise</em> statements on the basis of this map alone.</p>
<p>Another exercise: What visualization technique could you use if you wanted to make precise distinctions? (Leave your answer in the comments below.)</p>
<h2>80% of this is just formatting</h2>
<p>If you’re a beginner with , the code might look a little complex.</p>
<p>It’s actually much simpler than it seems at first glance.</p>
<p>About 80% of the code for the finalized chart is just <em>formatting code</em>. 80% just deals with things like the <a href="http://sharpsightlabs.com/blog/format-titles-and-axes-in-ggplot2/">font formatting</a>, the legend position, the text colors, etc. </p>
<p>We can actually strip away a lot of that formatting code and still create a functional map.</p>
<p>Here’s a stripped down version:</p>
<pre class="wp-code-highlight">
ggplot() +
  geom_polygon(data = map.county_unemp, aes(x = long, y = lat, group = group, fill = unemployed_rate))

</pre>
<p><img sizes="(max-width: 1438px) 100vw, 1438px" alt="" class="wp-image-3031" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/unemployment_US_choropleth-map_2016-nov_RAW.png?resize=456%2C287" data-recalc-dims="1"></p>
<p>We can strip the map down to two lines of code. Two lines of code are the core. Two lines of code do the “heavy lifting” to create the map … the rest is just formatting.</p>
<p>So, the good news is that if you want to begin making maps like these, you can get started quickly by learning only a couple lines of code.</p>
<h1>Sign up to master data visualization</h1>
<p>To become a data scientist, you need to master data visualization.</p>
<p>We’ll show you how.</p>
<p>Sign up now, and we’ll show you step-by-step how to learn (and master) data visualization in R.</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/">
<p><strong>Sign up now</strong></p>
<p></p></a>
<p>The post <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/map-unemployment-nov-2016/">Mapping unemployment data, 2016</a> appeared first on <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/">SHARP SIGHT LABS</a>.</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://sharpsightlabs.com/blog/map-unemployment-nov-2016/"> r-bloggers – SHARP SIGHT LABS</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/baufMRpmGxo"></a></div>