This workshop will introduce drawing maps and integrating statistical information in maps.
Maps can be plottet in a various of different ways, however, the easiest way is using ggplot and a function called ggplot.
We will use the ggmap to create maps.
There are 2 basic steps to making a map using ggmap:
First we will install and load the necessary packages:
install.packages("tibble")
install.packages("dplyr")
install.packages("magrittr")
install.packages("ggmap")
install.packages("tidyverse")
install.packages("maps")
install.packages("mapproj")
library(tibble) # The "tibble" datastructure.
library(dplyr) # Working with the tibble datastructure.
library(magrittr) # Need this for the %>% operator.
library(ggmap) # ggmap plots the raster objects
library(ggrepel)
library(tidyverse) # ggplot2 (visualisation), dplyr (manipulation), tidyr (tidying), readr (import) and more packages loaded in one
library(maps)
library(mapproj)
library(devtools) # For printing the session info at the end of the notebook.
library(gridExtra)
library(viridis)
library(knitr)
The get_map function provides a general approach for quickly obtaining maps from multiple sources.
There are different sources of map data.
stamen: easily accesible and widely used. maptype = c(“terrain”, “toner”, “watercolor”)
google: an api key and a account must be obtained. However it is free if you use less than 25.000 views / month maptype = c(“roadmap”, “terrain”, “satellite”, “hybrid”)
osm: open street map
cloudmade: an api key must be obtained, and it is free for the first 100.000 views, but there are detailed map information.
There are three different ways to define location:
location/address myLocation <- “Faroe Islands”
lat/long
myLocation <- c(lon = -6.9118061, lat = 61.8926353)
myLocation <- c(left = -7.866927, bottom = 61.230244, right = -5.91136, top = 62.605639)
mapFO <- get_stamenmap(bbox = c(left = -7.866927, bottom = 61.230244, right = -5.91136, top = 62.605639), zoom=8, maptype = "terrain", crop=FALSE) # crop = FALSE because otherwise the map is slightly shifted when we overlay data.
ggmap(mapFO)
Fine tune the scale or resolution of the map using zoom. The get_map function takes a guess at the zoom level, but you can alter it:
We choose zoom 9 to display Faroe Islands.
mapFO <- get_stamenmap(bbox = c(left = -7.866927, bottom = 61.230244, right = -5.91136, top = 62.605639), zoom = 9, maptype = "watercolor", crop=FALSE) # crop = FALSE because otherwise the map is slightly shifted when we overlay data.
ggmap(mapFO)
Here we changed the layout to look like a watercolor map. All styles can be seen at https://stamen.com. All maps can also be displayed in black and white
color = “bw”
UK <- map_data("world") %>% filter(region=="UK")
ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
theme_void() + ylim(50,59) + coord_map()
2 . Add points with latitude/longitude coordinates:
Using an open data sheet with latitude/longitude of all world cities and population data we can plot these data on the map.
The data contains information on name of city, country, population, latitude/longitude and if it is the capitol or not:
head(world.cities)
We filter the data son only UK cities are included:
dataUK=world.cities %>% filter(country.etc=="UK")
And then we plot the cities on the map:
ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point( data=dataUK, aes(x=long, y=lat)) +
theme_void() + ylim(50,59) + coord_map()
If we go back to Faroe Islands
FO <- map_data("world") %>% filter(region=="Faroe Islands") # To raster the map
ggplot() +
geom_polygon(data = FO, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
theme_void() + ylim(61,63) + xlim (-8,-6) + coord_map()
We see that this way of displaying the map is inadequate in resolution. This world database comes from a thinned cleaned-up version of the CIA World Data Bank II data and contains approximately 30,000 points representing the world coastlines and national boundaries. Apperently they did not save enough points to include all faroese islands. However, we will use it for plotting cities:
FO <- map_data("world") %>% filter(region=="Faroe Islands") # To raster the map
dataFO=world.cities %>% filter(country.etc=="Faroe Islands") # To filter cities from the world.cities database
ggplot() +
geom_polygon(data = FO, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point( data=dataFO, aes(x=long, y=lat)) +
theme_void() + ylim(61,63) + xlim (-8,-6) + coord_map()
We will change back to the stamen maps to continue to explore the possibilities:
Raster Faroe Islands with a zoom level of 9 in a water color mode:
map <- get_stamenmap( bbox = c(left = -7.866927, bottom = 61.230244, right = -5.91136, top = 62.605639), zoom = 9, maptype = "watercolor")
Plot Faroe Islands
ggmap(map) +
theme_void() +
theme(
plot.title = element_text(colour = "orange"),
panel.border = element_rect(colour = "grey", fill=NA, size=2)
)
Plot cities:
ggmap(map) + geom_point( data=dataFO, aes(x=long, y=lat, alpha=pop)) +
geom_point( data=dataFO, aes(x=long, y=lat), color="black", size=2) +
theme_void() + coord_map() +
theme_void() +
theme(
plot.title = element_text(colour = "orange"),
panel.border = element_rect(colour = "grey", fill=NA, size=2)
)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Plot top 10 largest cities with city names:
dataFO10 <- dataFO %>% arrange(pop) %>% tail(10)
ggmap(map) + geom_point( data=dataFO, aes(x=long, y=lat, alpha=pop)) +
geom_text_repel( data=dataFO10, aes(x=long, y=lat, label=name), size=4) +
geom_point( data=dataFO, aes(x=long, y=lat), color="black", size=1) +
geom_point( data=dataFO10, aes(x=long, y=lat), color="red", size=3) +
theme_void() + coord_map() +
theme(legend.position="none") +
theme(
plot.title = element_text(colour = "orange"),
panel.border = element_rect(colour = "grey", fill=NA, size=2)
)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Map cities as variables to size:
ggmap(map) + geom_point( data=dataFO, aes(x=long, y=lat, alpha=pop)) +
geom_point( data=dataFO, aes(x=long, y=lat, size=pop)) +
scale_size_continuous(range=c(1,12)) +
theme_void() + coord_map() +
theme(legend.position="none") +
theme(
plot.title = element_text(colour = "orange"),
panel.border = element_rect(colour = "grey", fill=NA, size=2)
)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Map cities as variables to colour:
ggmap(map) + geom_point( data=dataFO, aes(x=long, y=lat, alpha=pop)) +
geom_point( data=dataFO, aes(x=long, y=lat, color=pop, size=3)) +
theme_void() + coord_map() +
theme(legend.position="none") +
theme(plot.title = element_text(colour = "orange"),
panel.border = element_rect(colour = "grey", fill=NA, size=2)
)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
devtools::session_info()
─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
setting value
version R version 3.4.4 (2018-03-15)
os Ubuntu 16.04.6 LTS
system x86_64, linux-gnu
ui RStudio
language en_US
collate en_US.UTF-8
ctype en_US.UTF-8
tz Atlantic/Faroe
date 2019-08-26
─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
package * version date lib source
aod * 1.3.1 2019-01-26 [1] CRAN (R 3.4.4)
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.4.4)
backports 1.1.4 2019-04-10 [1] CRAN (R 3.4.4)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.4.4)
bitops 1.0-6 2013-08-17 [1] CRAN (R 3.4.4)
broom 0.5.2 2019-04-07 [1] CRAN (R 3.4.4)
callr 3.3.1 2019-07-18 [1] CRAN (R 3.4.4)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.4.4)
cli 1.1.0 2019-03-19 [1] CRAN (R 3.4.4)
colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.4.4)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.4.4)
curl 4.0 2019-07-22 [1] CRAN (R 3.4.4)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.4.4)
devtools * 2.1.0 2019-07-06 [1] CRAN (R 3.4.4)
digest 0.6.20 2019-07-04 [1] CRAN (R 3.4.4)
dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.4.4)
evaluate 0.14 2019-05-28 [1] CRAN (R 3.4.4)
fansi 0.4.0 2018-10-05 [1] CRAN (R 3.4.4)
forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.4.4)
fs 1.3.1 2019-05-06 [1] CRAN (R 3.4.4)
generics 0.0.2 2018-11-29 [1] CRAN (R 3.4.4)
ggmap * 3.0.0 2019-02-05 [1] CRAN (R 3.4.4)
ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.4.4)
ggrepel * 0.8.1 2019-05-07 [1] CRAN (R 3.4.4)
glue 1.3.1 2019-03-12 [1] CRAN (R 3.4.4)
gridExtra * 2.3 2017-09-09 [1] CRAN (R 3.4.4)
gtable 0.3.0 2019-03-25 [1] CRAN (R 3.4.4)
haven 2.1.1 2019-07-04 [1] CRAN (R 3.4.4)
hms 0.5.1 2019-08-23 [1] CRAN (R 3.4.4)
htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.4.4)
httr 1.4.1 2019-08-05 [1] CRAN (R 3.4.4)
jpeg 0.1-8 2014-01-23 [1] CRAN (R 3.4.4)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.4.4)
knitr * 1.24 2019-08-08 [1] CRAN (R 3.4.4)
labeling 0.3 2014-08-23 [1] CRAN (R 3.4.4)
lattice 0.20-38 2018-11-04 [4] CRAN (R 3.4.4)
lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.4.4)
lubridate * 1.7.4 2018-04-11 [1] CRAN (R 3.4.4)
magrittr * 1.5 2014-11-22 [1] CRAN (R 3.4.4)
mapproj * 1.2.6 2018-03-29 [1] CRAN (R 3.4.4)
maps * 3.3.0 2018-04-03 [1] CRAN (R 3.4.4)
MASS 7.3-50 2018-04-30 [4] CRAN (R 3.4.4)
Matrix 1.2-14 2018-04-09 [4] CRAN (R 3.4.4)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.4.4)
modelr 0.1.5 2019-08-08 [1] CRAN (R 3.4.4)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.4.4)
nlme 3.1-137 2018-04-07 [4] CRAN (R 3.4.4)
pillar 1.4.2 2019-06-29 [1] CRAN (R 3.4.4)
pkgbuild 1.0.4 2019-08-05 [1] CRAN (R 3.4.4)
pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.4.4)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.4.4)
plyr 1.8.4 2016-06-08 [1] CRAN (R 3.4.4)
png 0.1-7 2013-12-03 [1] CRAN (R 3.4.4)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.4.4)
processx 3.4.1 2019-07-18 [1] CRAN (R 3.4.4)
ps 1.3.0 2018-12-21 [1] CRAN (R 3.4.4)
purrr * 0.3.2 2019-03-15 [1] CRAN (R 3.4.4)
R6 2.4.0 2019-02-14 [1] CRAN (R 3.4.4)
Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.4.4)
readr * 1.3.1 2018-12-21 [1] CRAN (R 3.4.4)
readxl * 1.3.1 2019-03-13 [1] CRAN (R 3.4.4)
remotes 2.1.0 2019-06-24 [1] CRAN (R 3.4.4)
RgoogleMaps 1.4.4 2019-08-20 [1] CRAN (R 3.4.4)
rjson 0.2.20 2018-06-08 [1] CRAN (R 3.4.4)
rlang 0.4.0 2019-06-25 [1] CRAN (R 3.4.4)
rmarkdown 1.15 2019-08-21 [1] CRAN (R 3.4.4)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.4.4)
rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.4.4)
rvest 0.3.4 2019-05-15 [1] CRAN (R 3.4.4)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.4.4)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.4.4)
stringi 1.4.3 2019-03-12 [1] CRAN (R 3.4.4)
stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.4.4)
survival * 2.44-1.1 2019-04-01 [1] CRAN (R 3.4.4)
testthat 2.2.1 2019-07-25 [1] CRAN (R 3.4.4)
tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.4.4)
tidyr * 0.8.3 2019-03-01 [1] CRAN (R 3.4.4)
tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.4.4)
tidyverse * 1.2.1 2017-11-14 [1] CRAN (R 3.4.4)
usethis * 1.5.1 2019-07-04 [1] CRAN (R 3.4.4)
utf8 1.1.4 2018-05-24 [1] CRAN (R 3.4.4)
vctrs 0.2.0 2019-07-05 [1] CRAN (R 3.4.4)
viridis * 0.5.1 2018-03-29 [1] CRAN (R 3.4.4)
viridisLite * 0.3.0 2018-02-01 [1] CRAN (R 3.4.4)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.4.4)
xfun 0.9 2019-08-21 [1] CRAN (R 3.4.4)
xml2 1.2.2 2019-08-09 [1] CRAN (R 3.4.4)
yaml 2.2.0 2018-07-25 [1] CRAN (R 3.4.4)
zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.4.4)
[1] /home/olavur/R/x86_64-pc-linux-gnu-library/3.4
[2] /usr/local/lib/R/site-library
[3] /usr/lib/R/site-library
[4] /usr/lib/R/library