Heute haben Julius Tröger und Co mal wieder ein herrliches Stück Datenjournalismus veröffentlicht: http://interaktiv.morgenpost.de/berliner-zugezogenen-atlas/

Was mich allerdings spontan interessiert hat ist, ob es einen echten regionalen Trend gibt, oder ob der Quotient Zugezogene von einem Geburtsort durch Bevölkerung des Geburtsortes eher konstant ist.

Dies ist keine wissenschaftliche Untersuchung und kann auch keinen journalistischen Anspruch vorweisen. Ich habe mir bloß irgendwie die Bevölkerungsdaten besorgt. Diese könnten also unzuverlässig sein. Ich poste mein Werk vor allem, damit es jemand anderes noch mal ordentlich machen kann hint hint.

library(ggplot2)
library(ggmap)
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## Die folgenden Objekte sind maskiert von 'package:stats':
## 
##     filter, lag
## 
## Die folgenden Objekte sind maskiert von 'package:base':
## 
##     intersect, setdiff, setequal, union
library(knitr)

Ich verwende die Rohdaten von hier: https://docs.google.com/spreadsheets/d/12bt7nkEF7L5__1Ac6acqTB4hpyOBXl63cnRa2jYyCjM/edit#gid=0

Für diesen Test verwende ich nur die Daten von Städten mit > 1000 Zugezogenen.

trim <- function (x) gsub("^\\s+|\\s+$", "", x)

toberlin = read.csv('Rohdaten Berliner Zugezogenen-Atlas - Rohdaten.csv', stringsAsFactors = F) %>% filter(Zugezogene > 1000) %>% mutate(Geburtsort = trim(Geburtsort))
geocoded = geocode(sub('/Oberschlesien','',toberlin$Geburtsort))
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Aachen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Accra&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Adana&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Aleppo&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Altd%C3%B6bern&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Altenburg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Altlandsberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Angerm%C3%BCnde&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ankara&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Anklam&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Antakya&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Aschersleben&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Athen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Augsburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bad%20Saarow&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bafra&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bagdad&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Baku&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bamberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bangkok&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Barcelona&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bautzen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Beeskow&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Beirut&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Belzig&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bergen%20auf%20R%C3%BCgen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bergisch%20Gladbach&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Berlin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bernau%20bei%20Berlin&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bernburg%20(Saale)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bielefeld&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bijeljina&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Birecik&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bitterfeld-Wolfen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bochum&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bogota&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bonn&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bozova&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Brandenburg%20an%20der%20Havel&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Braunschweig&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bremen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bremerhaven&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Breslau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bromberg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Budapest&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Buenos%20Aires&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bukarest&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Burg%20(bei%20Magdeburg)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Celle&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Chemnitz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Cottbus&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Damaskus&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Danzig&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Darmstadt&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Demmin&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dessau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Detmold&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dnepropetrowsk&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dobrich&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dortmund&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dresden&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Duisburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=D%C3%BCsseldorf&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Eberswalde&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Eisenach&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Eisenh%C3%BCttenstadt&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Elsterwerda&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Erfurt&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Erlangen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Erzincan&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Essen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Falkensee&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Finsterwalde&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Flensburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Forst%20(Lausitz)&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Frankfurt%20(Oder)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Frankfurt%20am%20Main&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Freiberg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Freiburg%20im%20Breisgau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Fulda&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=F%C3%BCrstenwalde/Spree&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gardelegen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gaziantep&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gdingen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gelsenkirchen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gera&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gie%C3%9Fen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gleiwitz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=G%C3%B6rlitz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Gotha&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=G%C3%B6ttingen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Greifswald&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Guben&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=G%C3%BCstrow&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hagen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Halberstadt&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Halle%20(Saale)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hamburg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hameln&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hamm&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hannover&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hanoi&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Heidelberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Heilbronn&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Helmstedt&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hennigsdorf&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Herford&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Herne&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Herzberg%20(Elster)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hildesheim&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hinis&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Homs&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hoyerswerda&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Igdir&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Istanbul&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Izmir&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Jena&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kabul&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kairo&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Karaganda&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Karlsruhe&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kassel&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kattowitz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kelkit&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kiel&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kiew&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kleinmachnow&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Koblenz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=K%C3%B6ln&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=K%C3%B6nigs%20Wusterhausen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=K%C3%B6nigsberg%20(Preu%C3%9Fen)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Konstanz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=K%C3%B6then%20(Anhalt)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Krefeld&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kyritz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Landsberg%20an%20der%20Warthe&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lauchhammer&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Leipzig&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lima&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=L%C3%B6bau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lodz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=L%C3%BCbben%20(Spreewald)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=L%C3%BCbeck&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Luckenwalde&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ludwigsfelde&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ludwigshafen%20am%20Rhein&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ludwigslust&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=L%C3%BCneburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lutherstadt%20Wittenberg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Madrid&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Magdeburg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Mailand&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Mainz&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Mannheim&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Marburg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Meiningen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Mei%C3%9Fen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Merseburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Minden&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=M%C3%B6nchengladbach&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Moskau&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=M%C3%BChlhausen/Th%C3%BCringen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=M%C3%BCnchen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=M%C3%BCnster%20(Westfalen)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Nauen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Naumburg%20(Saale)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Neapel&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Neubrandenburg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Neum%C3%BCnster&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Neuruppin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Neustrelitz&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=New%20York&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Nghe%20An&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Nordhausen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=N%C3%BCrnberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Oberhausen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Odessa&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Offenbach%20am%20Main&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Oldenburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Omsk&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Oppeln&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Oranienburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Osnabr%C3%BCck&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Paderborn&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Parchim&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pasewalk&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Perleberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pforzheim&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pirna&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Plauen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Plovdiv&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Posen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Potsdam&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Poznan&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Prag&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Prenzlau&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pritzwalk&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Qu%E1%BA%A3ng%20B%C3%ACnh&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Quedlinburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Rathenow&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Recklinghausen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Regensburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Riesa&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Riga&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Rom&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Rostock&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=R%C3%BCdersdorf%20bei%20Berlin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Saarbr%C3%BCcken&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Salzgitter&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Salzwedel&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Samsun&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Sangerhausen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Sankt%20Petersburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=S%C3%A3o%20Paulo&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Sch%C3%B6nebeck%20(Elbe)&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Schwedt/Oder&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Schweinfurt&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Schwerin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Seoul&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Siegen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Sofia&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Spremberg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Stendal&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Stettin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Stralsund&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Strausberg&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Stuttgart&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Suhl&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Szczecin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Teheran&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Templin&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Tetovo&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Thessaloniki&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Torgau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Trier&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Tripoli&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=T%C3%BCbingen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Tuzla&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ueckerm%C3%BCnde&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Ulm&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Varna&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Varto&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Waren%20(M%C3%BCritz)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Warschau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Weimar&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wei%C3%9Fenfels&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wernigerode&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wien&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wiesbaden&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wilhelmshaven&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wismar&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wolfenb%C3%BCttel&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wolfsburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wolgast&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Woltersdorf%20(bei%20Berlin)&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wriezen&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wroclaw&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Wuppertal&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=W%C3%BCrzburg&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zagreb&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zehdenick&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zeitz&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zielona%20G%C3%B3ra&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zittau&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zossen&sensor=false
## .Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Zwickau&sensor=false

Die meisten Einwohnerdaten habe ich aus Wikipedia: https://de.wikipedia.org/wiki/Liste_der_Groß-_und_Mittelstädte_in_Deutschland#Gro.C3.9F-_und_Mittelst.C3.A4dte_nach_Einwohnerzahl

Einwohner = read.csv('Einwohner Wikipedia.csv', stringsAsFactors = F) %>%
  mutate(Name = trim(Name)) %>%
  mutate(X2014 = as.numeric(gsub("\\.", "", X2014))) %>%
  select(Geburtsort=Name, Einwohner=X2014)

missing = toberlin %>% left_join(Einwohner) %>% filter(is.na(Einwohner)) %>% select(Geburtsort)
## Joining by: "Geburtsort"
nrow(missing)
## [1] 125

Hier fehlen mir noch 125 welche ich versuche aus Wikipedia zu scrapen.

library(rvest)
## Loading required package: xml2
fetchPopWiki = function(query) {
  tryCatch({
    city <- read_html(paste0("https://www.wikipedia.de/go?q=",gsub(" ","+",query),"&l=de"))
    table = city %>% html_nodes("table.infobox") %>%
      html_table(fill=TRUE)
    tds = city %>% html_nodes('th,td')
    population = sub('([\\.0-9]+).*', '\\1', html_text(tds[grep('Einwohner|Bevölkerung', tds)[1] + 1])) # manchmal Bevölkerung
    num = as.numeric(gsub('\\.','',population))
    return(num)
  }, error=function(e) return(NA))
}
fetchPopWiki = Vectorize(fetchPopWiki)

fetchPopWiki(c('Bad Saarow', 'Accra', 'Moskau', 'Doesnt exist'))
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
##   Bad Saarow        Accra       Moskau Doesnt exist 
##         5090      2291352           NA           NA

Damit hole ich mir die meisten restlichen Werte - das was dann noch fehlt wird manuell recherchiert:

missingGeb = missing %>% select(Geburtsort) %>% mutate(Einwohner = fetchPopWiki(Geburtsort))
## Warning: ungenutzte Verbindung 5 (https://www.wikipedia.de/go?q=Gleiwitz/
## Oberschlesien&l=de) geschlossen
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning: ungenutzte Verbindung 5 (https://www.wikipedia.de/go?q=Nghe
## +An&l=de) geschlossen
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
## Warning in doTryCatch(return(expr), name, parentenv, handler): NAs durch
## Umwandlung erzeugt
stillMissingGeb = missingGeb %>% filter(is.na(Einwohner))

# Händisch Google
stillMissingGeb[stillMissingGeb$Geburtsort == 'Altlandsberg','Einwohner'] = 8769
stillMissingGeb[stillMissingGeb$Geburtsort == 'Bromberg','Einwohner'] = 362286
stillMissingGeb[stillMissingGeb$Geburtsort == 'Dessau','Einwohner'] = 88693
stillMissingGeb[stillMissingGeb$Geburtsort == 'Gleiwitz/Oberschlesien','Einwohner'] = 186347
stillMissingGeb[stillMissingGeb$Geburtsort == 'Königsberg (Preußen)','Einwohner'] = 437456
stillMissingGeb[stillMissingGeb$Geburtsort == 'Moskau','Einwohner'] = 11920000
stillMissingGeb[stillMissingGeb$Geburtsort == 'New York','Einwohner'] = 8406000
stillMissingGeb[stillMissingGeb$Geburtsort == 'Nghe An','Einwohner'] = 3037000
stillMissingGeb[stillMissingGeb$Geburtsort == 'Omsk','Einwohner'] = 1159000
stillMissingGeb[stillMissingGeb$Geburtsort == 'Oppeln/Oberschlesien','Einwohner'] = 122120
stillMissingGeb[stillMissingGeb$Geburtsort == 'Sankt Petersburg','Einwohner'] = 4991000
stillMissingGeb[stillMissingGeb$Geburtsort == 'Tripoli','Einwohner'] = 127611

Jetzt werden alle Daten verschnitten (und zwei Fehler bei den Einwohnerdaten repariert):

Einwohner = Einwohner %>% rbind(missingGeb %>% filter(!is.na(Einwohner))) %>% rbind(stillMissingGeb)

toberlin = toberlin %>% left_join(Einwohner)
## Joining by: "Geburtsort"
toberlin[toberlin$Geburtsort == 'Hanoi','Einwohner'] = 7233000
toberlin[toberlin$Geburtsort == 'Teheran','Einwohner'] = 8154000

toberlin[is.na(toberlin$Einwohner),]
##     Geburtsort Zugezogene Einwohner
## 200 Quảng Bình       1012        NA
## 250       Wien       2995        NA
toberlingeo = cbind(toberlin, geocoded)

Zuletzt berechne ich für jeden Ort den Abstand zu Berlin und die eigentlich gesuchte Kennzahl als Zugezogene.pro.Einwohner.Geburtsort.

library(geosphere)
## Loading required package: sp
berlin = geocode('Berlin')
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Berlin&sensor=false
distances = distHaversine(berlin, toberlingeo %>% select(lon,lat))
toberlingeo$Distanz = distances
toberlingeo = toberlingeo %>% mutate(Zugezogene.pro.Einwohner.Geburtsort = Zugezogene/Einwohner)

Damit kann ich mir das ganze jetzt geographish ansehen:

ggmap(get_map(location=berlin, zoom=6)) + geom_point(aes(x=lon, y=lat, size=Zugezogene.pro.Einwohner.Geburtsort), color='red', alpha=.5, data=toberlingeo)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=52.520007,13.404954&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Warning: Removed 68 rows containing missing values (geom_point).

?get_map

Auf den ersten Blick ist die Aussage der Karte: Berlin saugt Brandenburg leer. Nähe zu Berlin macht Berlin also noch unwiderstehlicher. Das ganze kann man auch im Scatterplot ganz gut sehen:

ggplot(toberlingeo, aes(x=Distanz, y=Zugezogene.pro.Einwohner.Geburtsort)) +
  scale_x_continuous(limits=c(0,2500000)) + geom_point() + geom_smooth()
## Warning: Removed 30 rows containing non-finite values (stat_smooth).
## Warning: Removed 30 rows containing missing values (geom_point).

Es gibt allerdings ein paar interessanten Ausreißer bei 2500km - die mache ich Sichtbar indem ich den Quotienten mit der Distanz multipliziere:

ggplot(toberlingeo, aes(x=Distanz, y=Zugezogene.pro.Einwohner.Geburtsort*Distanz)) +
   geom_point()
## Warning: Removed 2 rows containing missing values (geom_point).

Es scheint so, als würde bestimmten Gemeinden in der Türkei Berlin sehr gut gefallen…

toberlingeo %>% mutate(zpegd=Zugezogene.pro.Einwohner.Geburtsort*Distanz) %>% arrange(desc(zpegd)) %>% head(4) %>% kable
Geburtsort Zugezogene Einwohner lon lat Distanz Zugezogene.pro.Einwohner.Geburtsort zpegd
Hinis 2317 9803 41.70942 39.35964 2609160 0.2363562 616691.3
Bozova 1636 12657 38.52647 37.36148 2577375 0.1292565 333142.5
Varto 1058 9876 41.45597 39.17329 2607718 0.1071284 279360.7
Kelkit 1006 13417 39.43226 40.12875 2408904 0.0749795 180618.4

Das sollte man aber noch mal genauer anschauen.