ECON 413
Maps

Erol Taymaz
Department of Economics
Middle East Technical University

Mapping in R

Mapping data

Load libraries

library(ggplot2)
library(rgdal)
library(dplyr)
library(data.table)
library(haven)
library(gganimate)

Download the data files from Natural Earth

wa <- readOGR(dsn="Maps/ne_10m_admin_0_countries.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "/home/etaymaz/Documents/Econ/ECON 413/T2020/econ413/Presentations/Maps/ne_10m_admin_0_countries.shp", layer: "ne_10m_admin_0_countries"
## with 254 features
## It has 63 fields
class(wa)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"

Spatial objects like the _lndwa object are made up of a number of different slots, the key slots being @data (non-geographic attribute data) and @polygons (or @lines for line data).

head(wa@data)
##   scalerank      featurecla labelrank     sovereignt sov_a3 adm0_dif level
## 0         3 Admin-0 country         5    Netherlands    NL1        1     2
## 1         0 Admin-0 country         3    Afghanistan    AFG        0     2
## 2         0 Admin-0 country         3         Angola    AGO        0     2
## 3         3 Admin-0 country         6 United Kingdom    GB1        1     2
## 4         0 Admin-0 country         6        Albania    ALB        0     2
## 5         3 Admin-0 country         6        Finland    FI1        1     2
##                type       admin adm0_a3 geou_dif     geounit gu_a3 su_dif
## 0           Country       Aruba     ABW        0       Aruba   ABW      0
## 1 Sovereign country Afghanistan     AFG        0 Afghanistan   AFG      0
## 2 Sovereign country      Angola     AGO        0      Angola   AGO      0
## 3        Dependency    Anguilla     AIA        0    Anguilla   AIA      0
## 4 Sovereign country     Albania     ALB        0     Albania   ALB      0
## 5           Country       Aland     ALD        0       Aland   ALD      0
##       subunit su_a3 brk_diff        name     name_long brk_a3    brk_name
## 0       Aruba   ABW        0       Aruba         Aruba    ABW       Aruba
## 1 Afghanistan   AFG        0 Afghanistan   Afghanistan    AFG Afghanistan
## 2      Angola   AGO        0      Angola        Angola    AGO      Angola
## 3    Anguilla   AIA        0    Anguilla      Anguilla    AIA    Anguilla
## 4     Albania   ALB        0     Albania       Albania    ALB     Albania
## 5       Aland   ALD        0       Aland Aland Islands    ALD       Aland
##   brk_group abbrev postal                    formal_en formal_fr note_adm0
## 0      <NA>  Aruba     AW                        Aruba      <NA>     Neth.
## 1      <NA>   Afg.     AF Islamic State of Afghanistan      <NA>      <NA>
## 2      <NA>   Ang.     AO  People's Republic of Angola      <NA>      <NA>
## 3      <NA>   Ang.     AI                         <NA>      <NA>      U.K.
## 4      <NA>   Alb.     AL          Republic of Albania      <NA>      <NA>
## 5      <NA>  Aland     AI                Åland Islands      <NA>      Fin.
##   note_brk   name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13
## 0     <NA>       Aruba     <NA>         4         2         2          9
## 1     <NA> Afghanistan     <NA>         5         6         8          7
## 2     <NA>      Angola     <NA>         3         2         6          1
## 3     <NA>    Anguilla     <NA>         6         6         6          3
## 4     <NA>     Albania     <NA>         1         4         1          6
## 5     <NA>       Aland     <NA>         4         1         4          6
##    pop_est gdp_md_est pop_year lastcensus gdp_year                    economy
## 0   103065     2258.0      -99       2010      -99       6. Developing region
## 1 28400000    22270.0      -99       1979      -99  7. Least developed region
## 2 12799293   110300.0      -99       1970      -99  7. Least developed region
## 3    14436      108.9      -99        -99      -99       6. Developing region
## 4  3639453    21810.0      -99       2001      -99       6. Developing region
## 5    27153     1563.0      -99        -99      -99 2. Developed region: nonG7
##                income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3 wb_a2
## 0 2. High income: nonOECD       -99    <NA>     AW    ABW    533   533    AW
## 1           5. Low income       -99    <NA>     AF    AFG    004   004    AF
## 2  3. Upper middle income       -99    <NA>     AO    AGO    024   024    AO
## 3  3. Upper middle income       -99    <NA>     AI    AIA    660   660   -99
## 4  4. Lower middle income       -99    <NA>     AL    ALB    008   008    AL
## 5    1. High income: OECD       -99    <NA>     AX    ALA    248   248   -99
##   wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb     continent
## 0   ABW    -99        ABW        ABW        -99        -99 North America
## 1   AFG    -99        AFG        AFG        -99        -99          Asia
## 2   AGO    -99        AGO        AGO        -99        -99        Africa
## 3   -99    -99        AIA        AIA        -99        -99 North America
## 4   ALB    -99        ALB        ALB        -99        -99        Europe
## 5   -99    -99        ALA        ALD        -99        -99        Europe
##   region_un       subregion                 region_wb name_len long_len
## 0  Americas       Caribbean Latin America & Caribbean        5        5
## 1      Asia   Southern Asia                South Asia       11       11
## 2    Africa   Middle Africa        Sub-Saharan Africa        6        6
## 3  Americas       Caribbean Latin America & Caribbean        8        8
## 4    Europe Southern Europe     Europe & Central Asia        7        7
## 5    Europe Northern Europe     Europe & Central Asia        5       13
##   abbrev_len tiny homepart
## 0          5    4      -99
## 1          4  -99        1
## 2          4  -99        1
## 3          4  -99      -99
## 4          4  -99        1
## 5          5    5      -99
sapply(wa@data, class)
##   scalerank  featurecla   labelrank  sovereignt      sov_a3    adm0_dif 
##   "integer" "character"   "numeric" "character" "character"   "numeric" 
##       level        type       admin     adm0_a3    geou_dif     geounit 
##   "numeric" "character" "character" "character"   "numeric" "character" 
##       gu_a3      su_dif     subunit       su_a3    brk_diff        name 
## "character"   "numeric" "character" "character"   "numeric" "character" 
##   name_long      brk_a3    brk_name   brk_group      abbrev      postal 
## "character" "character" "character" "character" "character" "character" 
##   formal_en   formal_fr   note_adm0    note_brk   name_sort    name_alt 
## "character" "character" "character" "character" "character" "character" 
##   mapcolor7   mapcolor8   mapcolor9  mapcolor13     pop_est  gdp_md_est 
##   "numeric"   "numeric"   "numeric"   "numeric"   "numeric"   "numeric" 
##    pop_year  lastcensus    gdp_year     economy  income_grp   wikipedia 
##   "numeric"   "numeric"   "numeric" "character" "character"   "numeric" 
##     fips_10      iso_a2      iso_a3      iso_n3       un_a3       wb_a2 
## "character" "character" "character" "character" "character" "character" 
##       wb_a3      woe_id  adm0_a3_is  adm0_a3_us  adm0_a3_un  adm0_a3_wb 
## "character"   "numeric" "character" "character"   "numeric"   "numeric" 
##   continent   region_un   subregion   region_wb    name_len    long_len 
## "character" "character" "character" "character"   "numeric"   "numeric" 
##  abbrev_len        tiny    homepart 
##   "numeric"   "numeric"   "numeric"
mean(wa@data$pop_est)
## [1] 26750018
mean(wa@data$gdp_md_est)
## [1] 276935.1

We can plot the data

ggplot(wa@data, aes(x=pop_est)) + geom_density() 

ggplot(wa@data, aes(x=pop_est)) + geom_density() + scale_x_log10()

… and the map

plot(wa)

Coordinate system

The Coordinate Reference System (CRS) of spatial objects defines where they are placed on the Earth’s surface.

proj4string(wa) 
## [1] "+proj=longlat +datum=WGS84 +no_defs"

Plotting maps with ggplot

ggplot(wa, aes(long, lat, group = group)) + geom_polygon()

ggplot(wa, aes(long, lat, group = group)) + geom_polygon() +
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

ggplot(wa, aes(long, lat, group = group)) + geom_polygon() +
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T) +
   ylim(-60, 85)

wExc <- subset(wa, admin!="Antarctica")

ggplot(wExc, aes(long, lat, group = group)) + geom_polygon() + 
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

wTr<-subset(wa, admin=="Turkey")
ggplot(wTr, aes(long, lat, group = group)) + geom_polygon() +
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T) 

fortify method

wf <- fortify(wExc)
head(wf)
##        long      lat order  hole piece id group
## 1 -69.99694 12.57758     1 FALSE     1  0   0.1
## 2 -69.93639 12.53172     2 FALSE     1  0   0.1
## 3 -69.92467 12.51923     3 FALSE     1  0   0.1
## 4 -69.91576 12.49702     4 FALSE     1  0   0.1
## 5 -69.88020 12.45356     5 FALSE     1  0   0.1
## 6 -69.87682 12.42739     6 FALSE     1  0   0.1
wExc$id <- row.names(wExc)
wf <- left_join(wf, wExc@data)
head(wf)
##        long      lat order  hole piece id group scalerank      featurecla
## 1 -69.99694 12.57758     1 FALSE     1  0   0.1         3 Admin-0 country
## 2 -69.93639 12.53172     2 FALSE     1  0   0.1         3 Admin-0 country
## 3 -69.92467 12.51923     3 FALSE     1  0   0.1         3 Admin-0 country
## 4 -69.91576 12.49702     4 FALSE     1  0   0.1         3 Admin-0 country
## 5 -69.88020 12.45356     5 FALSE     1  0   0.1         3 Admin-0 country
## 6 -69.87682 12.42739     6 FALSE     1  0   0.1         3 Admin-0 country
##   labelrank  sovereignt sov_a3 adm0_dif level    type admin adm0_a3 geou_dif
## 1         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
## 2         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
## 3         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
## 4         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
## 5         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
## 6         5 Netherlands    NL1        1     2 Country Aruba     ABW        0
##   geounit gu_a3 su_dif subunit su_a3 brk_diff  name name_long brk_a3 brk_name
## 1   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
## 2   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
## 3   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
## 4   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
## 5   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
## 6   Aruba   ABW      0   Aruba   ABW        0 Aruba     Aruba    ABW    Aruba
##   brk_group abbrev postal formal_en formal_fr note_adm0 note_brk name_sort
## 1      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
## 2      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
## 3      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
## 4      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
## 5      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
## 6      <NA>  Aruba     AW     Aruba      <NA>     Neth.     <NA>     Aruba
##   name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est gdp_md_est pop_year
## 1     <NA>         4         2         2          9  103065       2258      -99
## 2     <NA>         4         2         2          9  103065       2258      -99
## 3     <NA>         4         2         2          9  103065       2258      -99
## 4     <NA>         4         2         2          9  103065       2258      -99
## 5     <NA>         4         2         2          9  103065       2258      -99
## 6     <NA>         4         2         2          9  103065       2258      -99
##   lastcensus gdp_year              economy              income_grp wikipedia
## 1       2010      -99 6. Developing region 2. High income: nonOECD       -99
## 2       2010      -99 6. Developing region 2. High income: nonOECD       -99
## 3       2010      -99 6. Developing region 2. High income: nonOECD       -99
## 4       2010      -99 6. Developing region 2. High income: nonOECD       -99
## 5       2010      -99 6. Developing region 2. High income: nonOECD       -99
## 6       2010      -99 6. Developing region 2. High income: nonOECD       -99
##   fips_10 iso_a2 iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us
## 1    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
## 2    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
## 3    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
## 4    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
## 5    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
## 6    <NA>     AW    ABW    533   533    AW   ABW    -99        ABW        ABW
##   adm0_a3_un adm0_a3_wb     continent region_un subregion
## 1        -99        -99 North America  Americas Caribbean
## 2        -99        -99 North America  Americas Caribbean
## 3        -99        -99 North America  Americas Caribbean
## 4        -99        -99 North America  Americas Caribbean
## 5        -99        -99 North America  Americas Caribbean
## 6        -99        -99 North America  Americas Caribbean
##                   region_wb name_len long_len abbrev_len tiny homepart
## 1 Latin America & Caribbean        5        5          5    4      -99
## 2 Latin America & Caribbean        5        5          5    4      -99
## 3 Latin America & Caribbean        5        5          5    4      -99
## 4 Latin America & Caribbean        5        5          5    4      -99
## 5 Latin America & Caribbean        5        5          5    4      -99
## 6 Latin America & Caribbean        5        5          5    4      -99
ggplot(wf, aes(long, lat, group = group, fill = gdp_md_est)) + 
   geom_polygon() + 
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

ggplot(wf, aes(long, lat, group = group, fill = -gdp_md_est)) + 
   geom_polygon() + 
   coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

ggplot(wf, aes(long, lat, group = group, fill = -log(gdp_md_est))) + 
    geom_polygon() +    
    coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

ggplot(wf, aes(long, lat, group = group, fill = income_grp)) + 
    geom_polygon() +         
    coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T)

ggplot(wf, aes(long, lat, group = group, fill = income_grp)) + 
    geom_polygon() +         
    coord_equal(ratio = 1.3, xlim = NULL, ylim = NULL, expand = T) +
    scale_fill_manual(values = c("darkred", "red", "pink", "orange", "brown" ))