<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/" data-counturl="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/" data-count="horizontal" data-text="Case Study: Mapping German ZIP Codes in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.arilamstein.com/blog/2016/05/16/use-case-study-mapping-german-zip-codes/"> R – AriLamstein.com</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>Shortly after running a webinar last week I received an email from Achim Rumberger, who attended the webinar live. Achim immediately put the webinar material into use for his own project, which involves mapping ZIP Codes in Germany. This was the first case study I received related to my new course, <a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL2NvdXJzZXMyLmFyaWxhbXN0ZWluLmNvbS9jb3Vyc2VzL3NoYXBlZmlsZXMtZm9yLXItcHJvZ3JhbW1lcnM%3D&feed-stats-url-post-id=2401">Shapefiles for R Programmers</a>, </em><em>and I wanted to share it with my readers.</em></p>
<p>I’m neuro-biologist by training, but for the past decade I make my living as a JAVA enterprise developer. I’m living and working in Germany, near Stuttgart, where Mercedes Benz cars and Porsches are manufactured.</p>
<p>Recently my daughter came to me with a problem – for her bachelor thesis she wants to see if certain kind of schools will benefit from the help of social services. So I suggested to her, to look for public available data, describing the social structure of the environment of these schools in question.</p>
<p>And all of a sudden, there was the problem to show some data in a geographical context.</p>
<p>Just at this time Ari published his webinar about getting shape files into R. Which also includes a introduction to shape files to get you going, if you are new to it, as I am. I remembered Ari from his mail course introducing his great R-package (choroplethr). By the way this is a terrible name, being a biologist by heart, I always type “chloroplethr”, as in “chlorophyll”, and this is not found by the R package manager. <em>[Editor’s note: I agree!]</em></p>
<p>Next question, where do I get the shapefiles, describing Germany? A major search engine was of great help here. <a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5zdWNoZS1wb3N0bGVpdHphaGwub3JnL2Rvd25sb2Fkcz9kb3dubG9hZD16dW9yZG51bmdfcGx6X29ydC5jc3Y%3D&feed-stats-url-post-id=2401">http://www.suche-postleitzahl.org/downloads?download=zuordnung_plz_ort.csv</a> . Germany has some 8700 zip code areas, so expect some time for rendering the file, if you do on your computer. Right on this side one can also find a dataset which might act as a useful warm up practice to display statistical data in a geographical context. Other sources are <a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cHM6Ly9kYXRhaHViLmlvL2RlL2RhdGFzZXQvcG9zdGFsLWNvZGVzLWRl&feed-stats-url-post-id=2401">https://datahub.io/de/dataset/postal-codes-de</a></p>
<p>And so I got started…</p>
<p>For development in R, I’m using RStudio, as its free, and most closely resembles the IDEs I know from JAVA development, though its functionality is a throwback to the 90s, compared to modern IDEs like Netbeans or IntelliJ. Especially in respect to refactoring and debugging.</p>
<p>To complete this task, there are whole lot of packages needed. But these will make life a whole lot easier. Look for the most recent packages, especially “readr”, which helps with loading the csv files into R. With “readr” you do not have to convert factors in strings again, as it is the case with the readcsv function from R.</p>
<p>The code itself is mostly copied and pasted from Ari’s webinar. It basically loads the shapefile into R, converts it into a data.frame and provides this data.frame with a variable called “region”. Then the stat data is loaded into R, again, the columns are renamed to something “choroplethr” recognizes (“region” and “values”), and finally “choroplethr” its doing it magic.</p>
<p>My hardware used is a MacBook from 2011 with 16GB RAM and an i7 Processor. The execution time for each of the plots is around 5 mins. R is using only a single core of the 4 available, but big memory helps.</p>
<p>And here is the code:</p>
<pre>
#make sure the workspace is in pristine condition
rm(list=ls(all=TRUE))
#shapefile from https://datahub.io/de/dataset/postal-codes-de
#http://www.suche-postleitzahl.org/downloads?download=zuordnung_plz_ort.csv
#post questions here: http://gis.stackexchange.com/
library(choroplethr)
library(dplyr)
library(ggplot2)
library(rgdal)
library(maptools)
library(gpclib)
library(readr)
library(R6)

setwd(&quot;&lt;path to your shape file>/plz-gebiete.shp/&quot;)
ger_plz &lt;- readOGR(dsn = &quot;.&quot;, layer = &quot;plz-gebiete&quot;)

gpclibPermit()
#convert the raw data to a data.frame as ggplot works on data.frames
ger_plz@data$id &lt;- rownames(ger_plz@data)
ger_plz.point &lt;- fortify(ger_plz, region=&quot;id&quot;)
ger_plz.df &lt;- inner_join(ger_plz.point,ger_plz@data, by=&quot;id&quot;)

head(ger_plz.df)
ggplot(ger_plz.df, aes(long, lat, group=group )) + geom_polygon()

#data file
df &lt;- read_csv(&quot;&lt;path to your stat data>/de_plz_einwohner.csv&quot;)
# variable name 'region' is needed for choroplethr
ger_plz.df$region &lt;- ger_plz.df$plz
head(ger_plz.df)

#subclass choroplethr to make a class for your my need

GERPLZChoropleth &lt;- R6Class(&quot;GERPLZChoropleth&quot;,
    inherit = choroplethr:::Choropleth,
    public = list(
        initialize = function(user.df) {
            super$initialize(ger_plz.df, user.df)
        }
    )
)
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c(&quot;region&quot;, &quot;value&quot;)

#instantiate new class with data
c &lt;- GERPLZChoropleth$new(df)

#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = &quot;Comparison of number of Inhabitants per Zipcode in Germany&quot;
c$legend= &quot;Number of Inhabitants per Zipcode&quot;
c$set_num_colors(9)
c$render()
</pre>
<p>And here is the result:</p>
<p>What you can see from the map, is that zip codes in the north and east of Germany tend to be larger. Small size zip codes coincide with metropolitan areas in Germany.</p>
<p><a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTYvMDUvZ2VyX3ppcF9jb2Rlcy5wbmc%3D&feed-stats-url-post-id=2401"><img sizes="(max-width: 864px) 100vw, 864px" srcset="http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/ger_zip_codes.png?w=864 864w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/ger_zip_codes.png?resize=300%2C227 300w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/ger_zip_codes.png?resize=768%2C582 768w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/ger_zip_codes.png?resize=676%2C512 676w" alt="ger_zip_codes" class="wp-image-2417" src="http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/ger_zip_codes.png?resize=676%2C512" data-recalc-dims="1"></a></p>
<p>Note that with choroplethr, it is easy to combine this choropleth map with a google map. This is useful for people who aren’t already familiar with German geography:</p>
<pre>
c$render_with_reference_map()
</pre>
<p><a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL3d3dy5hcmlsYW1zdGVpbi5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTYvMDUvdW5uYW1lZC0xLnBuZw%3D%3D&feed-stats-url-post-id=2401"><img sizes="(max-width: 1076px) 100vw, 1076px" srcset="http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?w=1076 1076w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?resize=300%2C220 300w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?resize=768%2C563 768w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?resize=1024%2C751 1024w, http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?resize=676%2C496 676w" alt="unnamed-1" class="wp-image-2420" src="http://i1.wp.com/www.arilamstein.com/wp-content/uploads/2016/05/unnamed-1.png?resize=676%2C496" data-recalc-dims="1"></a></p>
<p><em>Want to learn how to do projects like this? Take my course <a target="_blank" href="http://www.arilamstein.com/?feed-stats-url=aHR0cDovL2NvdXJzZXMyLmFyaWxhbXN0ZWluLmNvbS9jb3Vyc2VzL3NoYXBlZmlsZXMtZm9yLXItcHJvZ3JhbW1lcnM%3D&feed-stats-url-post-id=2401">Shapefiles for R Programmers</a>.</em></p>
<p> <img height="1" width="1" src="http://www.arilamstein.com/?feed-stats-post-id=2401">
<p>The post <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/blog/2016/05/16/use-case-study-mapping-german-zip-codes/">Case Study: Mapping German ZIP Codes in R</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.arilamstein.com/">AriLamstein.com</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/" data-counturl="http://www.r-bloggers.com/case-study-mapping-german-zip-codes-in-r/" data-count="vertical" data-text="Case Study: Mapping German ZIP Codes in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.arilamstein.com/blog/2016/05/16/use-case-study-mapping-german-zip-codes/"> R – AriLamstein.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Fay82MfBAug"></a></div>