For this exercise, we will need to packages: ggplot2 and ggmap

setwd("~/Dropbox/ExplDataAnalysisR/Olivia") #Set your working directory
#install.packages("ggplot2") #to install ggplot2 if you don't have it yet
#install.packages("ggmap") #to install ggmap if you don't have it yet
library(ggplot2) #load the library
## Warning: package 'ggplot2' was built under R version 3.2.3
library(ggmap) #load the library
## Warning: package 'ggmap' was built under R version 3.2.3

Here, we will import a map of Europe from Google Maps

#Get a map of Europe with the right zoom level (an integer from 3 (continent) to 21 (building), default value 10 (city))
europe_map<-get_map(location='Europe', zoom=4)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Europe&zoom=4&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Europe&sensor=false
#show the map
ggmap(europe_map)

Here, we will create a dataframe of all the city names. The geocode() function will find the find latitude and longitude of the city from Google Maps.

#The geocode function gets the longitude and latitude for each city
le <- geocode("london, england")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=london,%20england&sensor=false
pf <- geocode("paris, france")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=paris,%20france&sensor=false
lp <- geocode("lisbon, portugal")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=lisbon,%20portugal&sensor=false
ms <- geocode("madrid, spain")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=madrid,%20spain&sensor=false
es <- geocode("edinburgh, scotland")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=edinburgh,%20scotland&sensor=false
ni <- geocode("naples, italy")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=naples,%20italy&sensor=false
va <- geocode("vienna, austria")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=vienna,%20austria&sensor=false
ag <- geocode("athens, greece")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=athens,%20greece&sensor=false
mr <- geocode("moscow, russia")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=moscow,%20russia&sensor=false
mg <- geocode("munich, germany")
## .
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=munich,%20germany&sensor=false
pc <- geocode("prague, czech republic")
## .
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=prague,%20czech%20republic&sensor=false
di <- geocode("dublin, ireland")
## .
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=dublin,%20ireland&sensor=false
df <- data.frame(rbind(le, pf, lp, ms, es, ni, va, ag, mr, mg, pc, di)) 

Let’s connect the cities and add names to those that don’t appear on the map using the longitude and latitude geocodes

 #pdf("Europe_ExcelsiorMap.pdf")
ggmap(europe_map) +
labs(x = "Longitude", y = "Latitude") +
    #A white line through the cities    
  geom_path(aes(x = lon, y = lat), data = df, colour = "white", size = .8) +
    #A slightly thinner black line through the cities    
  geom_path(aes(x = lon, y = lat), data = df, colour = "black", size = .5) +
    #A black dot in every city
    geom_point(aes(x = lon, y = lat), data = df, colour = "black", size = 2) +
     #A smaller red dot in every city
  geom_point(aes(x = lon, y = lat), data = df, colour = "red", size = 1) +
  #Adding the name for Lisbon
  annotate("text", x = -9, y = 38, size = 2.5, label = "Lisbon") +
  #Adding the name for Edinburgh
  annotate("text", x = -3, y = 56.5, size = 2.6, label = "Edinburgh") +
  #Adding the name for Naples
  annotate("text", x = 16.2, y = 40.9, size = 2.6, label = "Naples") +     
  #Adding the name for Athens
  annotate("text", x = 23.8, y = 37.3, size = 2.6, label = "Athens") +     
  #Adding the name for Munich
  annotate("text", x = 9.7, y = 48.2, size = 2.6, label = "Munich") +
  #Adding the name for Dublin
  annotate("text", x = -7, y = 53.8, size = 2.6, label = "Dublin") +
  #Adding some text
  annotate("text", x = -3, y = 66, size = 5, label = "The surprising order of")+
  annotate("text", x = -3, y = 65, size = 5, label = "street names in the")+
  annotate("text", x = -3, y = 64, size = 5, label = "Excelsior neighborhood")+
  annotate("text", x = -3, y = 63, size = 5, label = "in San Francisco")

#dev.off()