<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/how-to-map-geospatial-data-usa-rivers/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/map-geospatial-data-usa-rivers/"> 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><a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1594px) 100vw, 1594px" alt="" class="wp-image-3036" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/rivers-and-waterways_USA_v2.png?resize=456%2C318" data-recalc-dims="1"><br>
</a></p>
<h1>R Code</h1>
<p>Here’s the R code to produce the map:</p>
<pre class="wp-code-highlight">
#===============
# LOAD PACKAGES
#===============
library(tidyverse)
library(maptools)


#===============
# GET RIVER DATA
#===============

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

#DEFINE URL
# - this is the location of the file
url.river_data &lt;- url(&quot;http://sharpsightlabs.com/wp-content/datasets/usa_rivers.RData&quot;)


# LOAD DATA
# - this will retrieve the data from the URL
load(url.river_data)


# INSPECT
summary(lines.rivers)
lines.rivers@data %>% glimpse()


levels(lines.rivers$FEATURE)
table(lines.rivers$FEATURE)

#==============================================
# REMOVE MISC FEATURES
# - there are some features in the data that we
#   want to remove
#==============================================
lines.rivers &lt;- subset(lines.rivers, !(FEATURE %in% c(&quot;Shoreline&quot;
                                                      ,&quot;Shoreline Intermittent&quot;
                                                      ,&quot;Null&quot;
                                                      ,&quot;Closure Line&quot;
                                                      ,&quot;Apparent Limit&quot;
                                                      )))

# RE-INSPECT
table(lines.rivers$FEATURE)

#==============
# REMOVE STATES
#==============

#-------------------------------
# IDENTIFY STATES
# - we need to find out
#   which states are in the data
#-------------------------------
table(lines.rivers$STATE)


#---------------------------------------------------------
# REMOVE STATES
# - remove Alaska, Hawaii, Puerto Rico, and Virgin Islands
# - these are hard to plot in a confined window, so 
#   we'll remove them for convenience
#---------------------------------------------------------

lines.rivers &lt;- subset(lines.rivers, !(STATE %in% c('AK','HI','PR','VI')))

# RE-INSPECT
table(lines.rivers$STATE)


#============================================
# FORTIFY
# - fortify will convert the 
#   'SpatialLinesDataFrame' to a proper
#    data frame that we can use with ggplot2
#============================================

df.usa_rivers &lt;- fortify(lines.rivers)


#============
# GET USA MAP
#============
map.usa_country &lt;- map_data(&quot;usa&quot;)
map.usa_states &lt;- map_data(&quot;state&quot;)


#=======
# PLOT
#=======

ggplot() +
  geom_polygon(data = map.usa_country, aes(x = long, y = lat, group = group), fill = &quot;#484848&quot;) +
  geom_path(data = df.usa_rivers, aes(x = long, y = lat, group = group), color = &quot;#8ca7c0&quot;, size = .08) +
  coord_map(projection = &quot;albers&quot;, lat0 = 30, lat1 = 40, xlim = c(-121,-73), ylim = c(25,51)) +
  labs(title = &quot;Rivers and waterways of the United States&quot;) +
  annotate(&quot;text&quot;, label = &quot;sharpsightlabs.com&quot;, family = &quot;Gill Sans&quot;, color = &quot;#A1A1A1&quot;
           , x = -89, y = 26.5, size = 5) +
  theme(panel.background = element_rect(fill = &quot;#292929&quot;)
        ,plot.background = element_rect(fill = &quot;#292929&quot;)
        ,panel.grid = element_blank()
        ,axis.title = element_blank()
        ,axis.text = element_blank()
        ,axis.ticks = element_blank()
        ,text = element_text(family = &quot;Gill Sans&quot;, color = &quot;#A1A1A1&quot;)
        ,plot.title = element_text(size = 34)
        ) 

</pre>
<h1>Use this as practice</h1>
<p>If you’ve learned the <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/start-with-data-visualization-manipulation/">basics of data visualization</a> in R (namely, ggplot2) and you’re interested in <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/map-unemployment-nov-2016/">geospatial visualization</a>, use this as a small, narrowly-defined exercize to practice some intermediate skills.</p>
<p>There are at least three things that you can learn and practice with this visualization:</p>
<ol>
<li><strong>Learn about color:</strong> Part of what makes this visualization compelling are the colors. Notice that in the area surrounding the US, we’re not using pure black, but a dark grey. For the title, we’re not using white, but a medium grey. Also, notice that for the rivers, we’re not using “blue” but a very specific hexadecimal color. These are all deliberate choices. As an exercise, I highly recommend modifying the colors. Play around a bit and see how changing the colors changes the “feel” of the visualization.</li>
<li><strong>Learn to build visualizations in layers:</strong> I’ve emphasized this several times recently, but <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/r-package-think-about-visualization/">layering is an important principle</a> of data visualization. Notice that we’re layering the river data over the USA country map. As an exercise, you could also layer in the state boundaries between the country map and the rivers. To do this, you can use .</li>
<li><strong>Learn about ‘Spatial’ data:</strong> R has several classes for dealing with ‘geospatial’ data, such as ‘‘, ‘‘, and others. Spatial data is a whole different animal, so you’ll have to learn its structure. This example will give you a little experience dealing with it. </li>
</ol>
<h1>Iterate to get the details right</h1>
<p>What really makes this visualization work is the fine little details. In particular, the size of the lines and the colors. </p>
<p>The reality is that creating good-looking visualizations requires attention to the little details. </p>
<p>To get the details right for a plot like this, I recommend that you <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/r-line-chart-and-datavis-process/">build the visualization iteratively</a>. </p>
<p>Start with a simple version of just the map of the US.</p>
<pre class="wp-code-highlight">
ggplot() +
  geom_polygon(data = map.usa_country, aes(x = long, y = lat, group = group), fill = &quot;#484848&quot;)

</pre>
<p><img sizes="(max-width: 1604px) 100vw, 1604px" alt="" class="wp-image-3037" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/map_usa_simple.png?resize=456%2C330" data-recalc-dims="1"></p>
<p>Next, layer on the rivers:</p>
<pre class="wp-code-highlight">
ggplot() +
  geom_polygon(data = map.usa_country, aes(x = long, y = lat, group = group), fill = &quot;#484848&quot;) +
  geom_path(data = df.usa_rivers, aes(x = long, y = lat, group = group)) 
</pre>
<p><img sizes="(max-width: 1600px) 100vw, 1600px" alt="" class="wp-image-3038" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/rivers_and_waterways_unformatted.png?resize=456%2C329" data-recalc-dims="1"></p>
<p>Make no mistake: this doesn’t look good. But, in the early stages, that’s not the goal. You just want to make sure that the data are structurally right. You want something simple that you can build on.</p>
<p>Ok, next, play with the river colors.</p>
<p>Start with a simple ‘‘:</p>
<pre class="wp-code-highlight">
ggplot() +
  geom_polygon(data = map.usa_country, aes(x = long, y = lat, group = group), fill = &quot;#484848&quot;) +
  geom_path(data = df.usa_rivers, aes(x = long, y = lat, group = group), color = &quot;blue&quot;) 

</pre>
<p><img sizes="(max-width: 1448px) 100vw, 1448px" alt="" class="wp-image-3039" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/rivers_and_waterways_blue.png?resize=456%2C304" data-recalc-dims="1"></p>
<p>Let’s be honest. This still does not look good.</p>
<p>But it’s closer.</p>
<p>From here, you can play with the colors some more. Select a new color (I recommend using a <a target="_blank" rel="nofollow" href="http://www.w3schools.com/colors/colors_picker.asp">color picker</a>), and modify the  aesthetic for .</p>
<pre class="wp-code-highlight">
ggplot() +
  geom_polygon(data = map.usa_country, aes(x = long, y = lat, group = group), fill = &quot;#484848&quot;) +
  geom_path(data = df.usa_rivers, aes(x = long, y = lat, group = group), color = &quot;#99ccff&quot;) 

</pre>
<p><img sizes="(max-width: 1448px) 100vw, 1448px" alt="" class="wp-image-3040" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/rivers_and_waterways_light-blue.png?resize=456%2C300" data-recalc-dims="1"></p>
<p>Not perfect, but better still.</p>
<p>From here, you can continue to iterate, add more details, and get them all “perfect”:</p>
<ul>
<li>The exact color (this takes lots of trial-and-error, and a bit of good taste)</li>
<li>The  size for </li>
<li>The title and text annotations</li>
<li>Modify the projection, and change it to the “albers” projection with </li>
<li>The other <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/format-titles-and-axes-in-ggplot2/"> details</a> like background color, removing extraneous elements (like the axis labels) etc</li>
</ul>
<p>Once again: getting this just right takes lots of iteration. Try it yourself and build this visualization from the bottom up.</p>
<h1>Learn  (because  makes this easy)</h1>
<p>In this post, we’ve used  to create this particular visualization. While I would classify this visualization at an “intermediate” level, ggplot2 still makes it relatively easy.</p>
<p>That said, if you’re interested in data science and data visualization, learn .</p>
<p>Longtime readers at <a href="http://sharpsightlabs.com">Sharp Sight</a> will know my thoughts on this, but if you’re a new reader this is important.</p>
<p> is almost without question, the <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/r-recommend-data-science/">best data visualization tool available</a>. Of course, different people will have different needs, but speaking generally,  is flexible, powerful, and it allows you to create beautiful data visualizations with relative ease.</p>
<p>Not interested in visualization per se? </p>
<p>Do you want to focus on machine learning instead? </p>
<p>Fair enough.</p>
<p>If you want to <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/quick-introduction-machine-learning-r-caret/">learn machine learning</a>, you still need to be able to <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/data-analysis-machine-learning-example-1/">analyze and explore your data</a>. </p>
<p>Once again, the best tool for exploring and analyzing your data is . This is particularly true when you <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">combine it with </a>, , , and other tools from the .</p>
<h1>Sign up to master data visualization</h1>
<p>Do you want to get a job as a data scientist?</p>
<p>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-geospatial-data-usa-rivers/">How to map geospatial data: USA rivers</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-geospatial-data-usa-rivers/"> 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/be6WSo4qc24"></a></div>