<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/simple-practice-basic-maps-with-the-tidyverse/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/simple-practice-maps-tidyverse/"> 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>To <a href="http://sharpsightlabs.com/blog/are-you-fluent-r/">master data science</a>, you need to <strong>practice</strong>.</p>
<p>This sounds easy enough, but in reality, many people have no idea <em>how</em> to practice.</p>
<p>A full explanation of <a href="http://sharpsightlabs.com/blog/one-thing-master-data-science/">how to practice</a> is beyond the scope of this blog post, but I can give you a quick tip here:</p>
<p>You need to master the most important techniques, and then practice those techniques in small scripts until you know them backwards and forwards. </p>
<p>Your goal should be to be able to write code “with your eyes closed.”</p>
<p>With that in mind, I want to give you a few small scripts that you can practice. Here are a few small scripts to create a set of maps. (As you examine these, pay close attention to how simple they are. How many functions and commands do you actually need to know?)</p>
<p>Let’s start with a simple one: create a map of the United States.</p>
<pre class="wp-code-highlight">
# INSTALL PACKAGE: tidyverse
library(tidyverse)

# MAP USA
map_data(&quot;usa&quot;) %>% 
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()

</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/map_usa-600x369.png?w=450" alt="" class="wp-image-3096" data-recalc-dims="1"><br>
</a></p>
<p>Three lines of code (four, if you count the  function).</p>
<p>That’s it. Three lines to make a map of the US.</p>
<p>And if you change just 5 characters (change  to ) you can generate a map of the states of the United States:</p>
<pre class="wp-code-highlight">
# MAP USA (STATES)
map_data(&quot;state&quot;) %>% 
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/map_usa_states-600x358.png?w=450" alt="" class="wp-image-3097" data-recalc-dims="1"><br>
</a></p>
<p>If you add an additional line, you can use  to subset your data and map only a few states.</p>
<pre class="wp-code-highlight">
# MAP CALIFORNIA, NEVADA, OREGON, WASHINGTON
# - to do this, we're using dplyr::filter()
#   ... otherwise, it's almost exactly the same as the previous code
map_data(&quot;state&quot;) %>% 
  filter(region %in% c(&quot;california&quot;,&quot;nevada&quot;,&quot;oregon&quot;,&quot;washington&quot;)) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 244px) 100vw, 244px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/map_western_USA_states-367x600.png?resize=244%2C400" alt="" class="wp-image-3098" data-recalc-dims="1"><br>
</a></p>
<p>What I want to emphasize is how <em>easy</em> this is if you just know how  works and how the pipe operator () works.</p>
<p>If you make another simple change, you can create a county level map of those states:</p>
<pre class="wp-code-highlight">
# MAP CALIFORNIA, NEVADA, OREGON, WASHINGTON
#  (Counties)

map_data(&quot;county&quot;) %>% 
  filter(region %in% c(&quot;california&quot;,&quot;nevada&quot;,&quot;oregon&quot;,&quot;washington&quot;)) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 244px) 100vw, 244px" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/county_map_western_states-367x600.png?resize=244%2C400" alt="" class="wp-image-3101" data-recalc-dims="1"><br>
</a></p>
<p>Finally, a few more changes can get you a map of the world:</p>
<pre class="wp-code-highlight">
# MAP WORLD

map_data(&quot;world&quot;) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/map_world-600x354.png?w=450" alt="" class="wp-image-3100" data-recalc-dims="1"><br>
</a></p>
<p>… and then a map of a single country, like Japan:</p>
<pre class="wp-code-highlight">
# MAP JAPAN

map_data(&quot;world&quot;) %>%
  filter(region == 'Japan') %>%
  ggplot(aes(x = long, y = lat, group = group)) +
    geom_polygon()
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img alt="" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/08/map_japan-600x540.png?resize=400%2C350" class="wp-image-3099" data-recalc-dims="1"><br>
</a></p>
<p>I want to point out again that all of these maps were created with very simple variations on our original 3 line script.</p>
<p>It really doesn’t take that much to achieve competence in creating basic maps like this.</p>
<h1>Practice small to learn big</h1>
<p>You might be asking: why bother practicing these? They’re not terribly useful.</p>
<p>You need to understand that <a href="http://sharpsightlabs.com/blog/one-thing-master-data-science/">large projects</a> are built from dozens of small snippets of code like these. </p>
<p>Moreover, these little snippets of code are made up of a <a href="http://sharpsightlabs.com/blog/how-to-create-a-scatterplot-in-r/">small set</a> of <a href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">functions</a> that you can break down and learn one command at a time. </p>
<p>So the path to mastery involves first mastering syntax of small individual commands and functions. After you’ve memorized the syntax of individual functions and commands, little scripts like this give you an opportunity to put those commands together into something a little more complex. </p>
<p>Later, these little scripts can be put together with other code snippets to perform more complicated analyses.</p>
<p>If you can practice (and master) enough small scripts like this, it becomes dramatically easier to <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-visualization/">execute larger projects</a> quickly and confidently.</p>
<p>For example, by mastering the little scripts above, you put yourself on a path to creating something more like this:</p>
<p><a href="http://sharpsightlabs.com/blog/map-unemployment-nov-2016/"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/01/unemployment_US_choropleth-map_2016-nov-1-600x377.png?w=450" alt="" class="wp-image-3030" data-recalc-dims="1"><br>
</a></p>
<p>If you want to achieve real fluency, you need to practice small before you practice big. So find little scripts like this, break them down, and drill them until you can type them without hesitation. You’ll thank me later.</p>
<p>(You’re welcome.)</p>
<h1>A quick exercise for you</h1>
<p>Count up the commands and arguments that we used in the little scripts above.</p>
<p>For the sake of simplicity, don’t count the data features that you might need (like the “region” column, etc), just count the number of functions, commands, and arguments that you need to know.</p>
<p>How many? If you really, really pushed yourself, how long would it take to memorize those commands?</p>
<p>Leave a comment below and tell me.</p>
<h1>Sign up now, and discover how to rapidly master data science</h1>
<p>To rapidly master data science, you need a plan.</p>
<p>You need to be highly systematic.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank"><strong>Sign up for our email list</strong></a> right now, and you’ll get our “Data Science Crash Course.”</p>
<p>In it you’ll discover:</p>
<ul>
<li>A step-by-step learning plan</li>
<li>How to create the essential data visualizations</li>
<li>How to perform the essential data wrangling techniques</li>
<li>How to get started with machine learning</li>
<li>How much math you need to learn (and when to learn it)</li>
<li>And more …</li>
</ul>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://sharpsightlabs.com/blog/simple-practice-maps-tidyverse/" target="_blank">Simple practice: basic maps with the Tidyverse</a> appeared first on <a rel="nofollow" href="http://sharpsightlabs.com/" target="_blank">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/simple-practice-maps-tidyverse/"> 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 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/WI5ALceo3eg" height="1"></a></div>