<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/10-million-dots-mapping-european-population/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://spatial.ly/2017/03/mapping-european-population/"> R – Spatial.ly</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Dot density maps are an increasingly popular way of showing population datasets. The technique has its limitations (see <a target="_blank" rel="nofollow" href="http://www.axismaps.com/guide/dot_density/">here</a> for more info), but it can create some really nice visual outputs, particularly when trying to show density. I have tried to push this to the limit by using a high resolution population grid for Europe (<a target="_blank" rel="nofollow" href="http://ec.europa.eu/eurostat/statistics-explained/index.php/Population_grids">GEOSTAT 2011</a>) to plot a dot for every 50 people in Europe (<a target="_blank" rel="nofollow" href="http://dataviz.spatial.ly/European_Population_Density.html">interactive</a>). The detailed data aren’t published for all European countries – there are modelled values for those missing but I have decided to stick to only those countries with actual counts here.<a target="_blank" rel="nofollow" href="http://dataviz.spatial.ly/European_Population_Density.html"><img sizes="(max-width: 1000px) 100vw, 1000px" alt="" class="wp-image-5881" src="https://i1.wp.com/spatial.ly/wp-content/uploads/2017/03/european_population_density.jpg?resize=4610%2C5270" data-recalc-dims="1"></a></p>
<h2>Giant dot density maps with R</h2>
<p>I produced this map as part of an experiment to see how R could handle generating millions of dots from spatial data and then plotting them. Even though the final image is 3.8 metres by 3.8 metres – this is as big as I could make it without causing R to crash – many of the grid cells in urban areas are saturated with dots so a lot of detail is lost in those areas. The technique is effective in areas that are more sparsely populated. It would probably work better with larger spatial units that aren’t necessarily grid cells.</p>
<p>Generating the dots (using the <a target="_blank" rel="nofollow" href="https://www.rdocumentation.org/packages/sp/versions/1.2-3/topics/spsample">spsample</a> function) and then plotting them in one go would require way more RAM than I have access to. Instead I wrote a loop to select each grid cell of population data, generate the requisite number of points and then plot them. This created a much lighter load as my computer happily chugged away for a few hours to produce the plot. I could have plotted a dot for each person (500 million+) but this would have completely saturated the map, so instead I opted for 1 dot for every 50 people.</p>
<p>I have provided full code below. T&amp;Cs on the data mean I can’t share that directly but you can <a target="_blank" rel="nofollow" href="http://ec.europa.eu/eurostat/statistics-explained/index.php/Population_grids">download here</a>.</p>
<p>Load <code>rgdal</code> and the spatial object required. In this case it’s the GEOSTAT 2011 population grid. In addition I am using the rainbow function to generate a rainbow palette for each of the countries.</p>
<div>
<pre><code>library(rgdal)

Input&lt;-readOGR(dsn=&quot;.&quot;, layer=&quot;SHAPEFILE&quot;)

Cols&lt;- data.frame(Country=unique(Input$Country), Cols=rainbow(nrow(data.frame(unique(Input$Country)))))</code></pre>
<p>Create the initial plot. This is empty but it enables the dots to be added interatively to the plot. I have specified 380cm by 380cm at 150 dpi PNG – this is as big as I could get it without crashing R.</p>
<pre><code>png(&quot;europe_density.png&quot;,width=380, height=380, units=&quot;cm&quot;, res=150)
par(bg='black')
plot(t(bbox(Input)), asp=T, axes=F, ann=F)</code></pre>
<p>Loop through each of the individual spatial units – grid cells in this case – and plot the dots for each. The number of dots are specified in spsample as the grid cell’s population value/50.</p>
<pre><code>for (j in 1:nrow(Cols))
{

Subset&lt;- Input[Input$Country==Cols[j,1],]

for (i in 1:nrow(Subset@data))
  {
  Poly&lt;- Subset[i,]
  pts1&lt;-spsample(Poly, n = 1+round(Poly@data$Pop/50), &quot;random&quot;, iter=15)
  #The colours are taken from the Cols object.
  plot(pts1, pch=16, cex=0.03, add=T, col=as.vector(Cols[j,2]))
  }
}
dev.off()</code></pre>
</div>
<p> </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://spatial.ly/2017/03/mapping-european-population/"> R – Spatial.ly</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/o2BSjlDcrxk"></a></div>