<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-france-at-night-with-the-new-sf-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/mapping-france-night/"> 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 href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1102px) 100vw, 1102px" alt="" class="wp-image-3069" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/03/france_population_density_small.png?w=450" data-recalc-dims="1"><br>
</a></p>
<p>A few days ago, I was inspired by a set of photographs of <a target="_blank" rel="nofollow" href="https://www.google.com/search?q=earth+at+night+from+space">Earth from space, at night</a>.</p>
<p>The images are amazing, so I decided to try to replicate them using R.</p>
<p>To be a little more specific, I found a single dataset – a data set with French population data – and I’m using it to replicate the images for a single country: France.</p>
<h2>A few notes and warnings before you get started</h2>
<p>As usual, I’ve included the code for how to create the final data visualization.</p>
<p>Having said that, in this case I decided to use some new tools. Therefore, before trying to run the code yourself, there are a few notes and caveats to keep in mind.</p>
<h3>This was built with the new  package</h3>
<p>I decided to build this with the new  package. </p>
<p> is a new package for geospatial analysis that’s currently under development. You can read more <a target="_blank" rel="nofollow" href="https://github.com/edzer/sfr">about it here</a>.</p>
<h3>Warning: setting up  and dependencies may be problematic</h3>
<p>Before getting started, you should know that installing  may require you to install some dependencies. You can find out more <a target="_blank" rel="nofollow" href="https://github.com/edzer/sfr">here</a>.</p>
<p>If you decide to install  and the dependencies, be warned: getting the dependencies set up (specifically, gdal) may be tricky and problematic, so attempt it <b>at your own risk</b>. </p>
<h1>Code: mapping French population density</h1>
<p>Ok, here’s the code.</p>
<p>First, we’ll load the packages that we’re going to need. (Of course, you need to have them installed as well.)</p>
<p>Notice also that we’re using the development version of . We need this because we’ll be using .</p>
<pre class="wp-code-highlight">
#=======================================
# INSTALL DEVELOPMENT VERSION OF ggplot2
#=======================================

library(devtools)
dev_mode(on = T)
install_github(&quot;hadley/ggplot2&quot;)

#==============
# LOAD PACKAGES
#==============
library(sf)
library(tidyverse)
library(stringr)
library(scales)

</pre>
<h3>Get the data</h3>
<p>Next, we’ll get the data from a URL at Sharp Sight and load it into R:</p>
<pre class="wp-code-highlight">
#=========
# GET DATA
#=========
url.france_pop &lt;- url(&quot;http://sharpsightlabs.com/wp-content/datasets/france_population_data_2016.RData&quot;)

load(url.france_pop)

# INSPECT
glimpse(df.france)

</pre>
<h3>Change column names to lower case</h3>
<p>We won’t have to make too many adjustments to the data, but we will transform the variable names to lower case.</p>
<pre class="wp-code-highlight">
#======================================
# CHANGE COLUMN NAMES: lowercase
# - in the original dataset the column
#   names were capitalized
# - we'll transform the names to 
#   lower case
#======================================
colnames(df.france) &lt;- colnames(df.france) %>% str_to_lower()


# INSPECT
colnames(df.france)

</pre>
<h3>Create  variable</h3>
<p>In the visualization, we’re going to plot population density. </p>
<p>The dataset does not have a density variable, so we need to calculate it based on  and  (i.e. area). Keep in mind that  is in hectares, so we’ll be converting from “people/hectare” to “people/square kilometer.”</p>
<pre class="wp-code-highlight">
#=========================
# CREATE DENSITY VARIABLE
#=========================
df.france$population %>% summary()


#-------------------------
# CREATE VARIABLE: density
#-------------------------
df.france &lt;- df.france %>% mutate(density = population/superficie*100)


# INSPECT  
colnames(df.france)
head(df.france)

</pre>
<h3>Plot a test chart</h3>
<p>Next, we’ll plot a very simple test chart to see if things are roughly OK.</p>
<p>As we do this, note how simple it is to use . This is one of the advantages of using the new  package.</p>
<pre class="wp-code-highlight">
#==========================================
# PLOT TEST CHART
# - here, we'll just plot a test chart
#   to see if the data are roughly correct
#   and to make sure that the data are
#   roughly in working order
#==========================================

#ggplot(df.france) + geom_sf()
ggplot(df.france) + geom_sf(aes(fill = density))

</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1456px) 100vw, 1456px" alt="" class="wp-image-3070" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/03/france_density__TEST.png?w=450" data-recalc-dims="1"><br>
</a></p>
<p>This appears to be OK, but it’s tough to tell because the borders are too thick.</p>
<p>Let’s do one more test chart to try to remove the lines and get a better look:</p>
<pre class="wp-code-highlight">
#==========================================
# PLOT SECOND TEST CHART
# - here, we'll plot another test with
#   smaller borders
# - we'll set size = .1
#==========================================

ggplot(df.france) + geom_sf(aes(fill = density), color = NA)


</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1510px) 100vw, 1510px" alt="" class="wp-image-3071" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/03/france_density__TEST2.png?w=450" data-recalc-dims="1"><br>
</a></p>
<p>Ok, we can see the fill color now (although the borders are still there).</p>
<p>Looking at this, we’ll have to solve at least 2 problems:</p>
<ul>
<li>Adjust the color scale</li>
<li>Remove the borders</li>
</ul>
<p>Most of the remaining work will be modifying the  color to get the colors right, and adjust the scale so that differences between densities are more visible.</p>
<h3>Calculate quantiles to identify color break points</h3>
<p>Ultimately, we need to adjust the fill-colors associated with the  aesthetic. We need to find “break points” to which we’ll associate specific fill values.</p>
<p>To do this, we’ll calculate quantiles. </p>
<pre class="wp-code-highlight">
#=======================================================
# IDENTIFY QUANTILES
# - the hardest part of creating the finalized
#   data visualization is getting the color scale
#   just right
# - on final analysis, the best way to adjust the color
#   scale was by capping the population density
# - we'll use these quantiles to find a good cap
#   via trial and error
#=======================================================


#---------
# VENTILES
#---------

quantile(df.france$density, seq(from = 0, to = 1, by = .05))

# 0%           5%          10%          15%          20%          25%          30% 
# 0.000000     6.815208    10.032525    12.824891    15.614076    18.485419    21.719516 

# 35%          40%          45%          50%          55%          60%          65% 
# 25.510974    29.538791    34.568568    40.088124    46.762867    54.489134    64.356473 

# 70%          75%          80%          85%          90%          95%         100% 
# 77.376987    94.117647   118.935653   160.578630   242.086849   510.070301 41929.234973


#----------------------
# PERCENTILES: 90 - 100
#----------------------

quantile(df.france$density, seq(from = .9, to = 1, by = .01))

# 90%        91%        92%        93%        94%        95%        96%        97%        98% 
# 242.0868   268.1167   300.2817   347.6989   412.2042   510.0703   637.9211   876.0090  1300.6435 
# 99%       100% 
#   2649.3653 41929.2350 


# MEDIAN
median(df.france$density) #40.08812

</pre>
<h3>Create finalized version of chart</h3>
<p>Let’s plot the finalized version. I’ll give a little explanation afterwards.</p>
<pre class="wp-code-highlight">
#==================
# DETAILED VERSION
# - add theme
# - add colors
#==================

df.france %>%
  mutate(fill_var = density) %>%
  ggplot() + 
    geom_sf(aes(fill = fill_var, color = fill_var)) +
    scale_fill_gradientn(colours = c(&quot;#000233&quot;,&quot;#f9cf86&quot;,&quot;#fceccf&quot;,&quot;white&quot;) 
                         ,values = rescale(c(0,500,3000,40000))
                         ,breaks = c(0,500,1500)
                         ,guide = FALSE) + 
    scale_color_gradientn(colours = c(&quot;#000233&quot;,&quot;#f9cf86&quot;,&quot;#fceccf&quot;,&quot;white&quot;) 
                          ,values = rescale(c(0,500,3000,40000))
                          ,guide = FALSE) +
    labs(title = &quot;Population density in France\nvisualized to imitate satellite images of France at night&quot; 
         ,subtitle = &quot;(people/sq km)&quot; ) +
    theme(text = element_text(family = &quot;Gill Sans&quot;, color = &quot;#E1E1E1&quot;)
          ,plot.title = element_text(size = 18, color = &quot;#E1E1E1&quot;)
          ,plot.subtitle = element_text(size = 10)
          ,plot.background = element_rect(fill = &quot;#000223&quot;)
          ,panel.background = element_rect(fill = &quot;#000223&quot;)
          ,panel.grid.major = element_line(color = &quot;#000223&quot;)
          ,axis.text = element_blank()
          ,axis.ticks = element_blank()
          ,legend.background = element_blank()
          )

#==================
# TURN OFF DEV MODE
#==================
dev_mode(on = F)

</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1102px) 100vw, 1102px" alt="" class="wp-image-3069" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/03/france_population_density_small.png?w=450" data-recalc-dims="1"><br>
</a></p>
<p>OK, the truth is that to create this finalized version, you actually need to do a lot of trial and error.</p>
<p>Essentially, you need to find the right cutoffs in the data and associate those values to specific fill values.</p>
<p>The point where we do that is in the following code:</p>
<pre class="wp-code-highlight">
    scale_fill_gradientn(colours = c(&quot;#000233&quot;,&quot;#f9cf86&quot;,&quot;#fceccf&quot;,&quot;white&quot;) 
                         ,values = rescale(c(0,500,3000,40000))
</pre>
<p>Here, we’re taking <em>values</em> of our fill variable, , and we’re associating those values to specific colors. How do we know which values to use and which colors to use? </p>
<p>Trial and error.</p>
<p>You need to take the quantile values that we calculated earlier, and test them out. Try different cutoff values for . Try different fill-values for . </p>
<h2>To create visualizations like this, master </h2>
<p>I’ll admit that this data visualization is a little challenging to create, simple because of the extensive trial and error that’s required.</p>
<p>It’s also a little out of the ordinary, because we’re using the some packages that are under development.</p>
<p>Having said that, this visualization is still essentially just a fancy application of . If you really know what you’re doing with  – if you’ve <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/stop-trying-jump-sexy-stuff/">already mastered the basics</a> – then this plot is fairly straightforward.</p>
<p>So, if you want to learn to create compelling data visualizations, master .</p>
<h1>Sign up now to master ggplot2</h1>
<p>To get a job as a highly-paid data scientist, you need to know how to create compelling data visualizations.</p>
<p>To create compelling visualizations in R, you need to master .</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><strong>Sign up now</strong></a> we’ll show you how.</p>
<p>By signing up, you’ll get our free <em>Data Science Crash Course</em>, where you’ll learn:</p>
<ul>
<li>the foundations of ggplot2</li>
<li>3 essential data visualizations</li>
<li>a step-by-step data science learning plan </li>
<li>the 1 programming language you need to learn </li>
<li>how to do data manipulation in R</li>
<li>how to get started with machine learning</li>
<li>and more …</li>
</ul>
<p>In addition, you’ll also get weekly data science tutorials, delivered right to your inbox.</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/mapping-france-night/">Mapping “France at night” with the new sf package</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/mapping-france-night/"> 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/d6lDe-4gFtc"></a></div>