class: center, middle, inverse, title-slide # Mapping Data in R with ggplot2 and geom_sf() ## a ‘flipbookr’ tutorial by Conner J. Surrency, January 2020 ### advice/editing Gina Reynolds --- # What Are We Doing? -- - Here we will walk through the process of creating maps in RStudio. -- - This will include the packages required and how to code a map. -- - We will be looking at data from two cases: Wetherspoon restaurants in UK, and defense expenditure of NATO member states. --- ## Set-Up -- We will need the following packages (and data) installed: -- - tidyverse -- - rnaturalearth --- #Example #1 -- We know the location of restaurants in the UK and the price of the empire burgur. The map we make will be a simple static map that shows those points. --- class: split-40 count: false .column[.content[ ```r *library(tidyverse) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) *library(rnaturalearth) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) *library(readxl) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) *read_excel("wetherspoon.xlsx") ``` ]] .column[.content[ ``` # A tibble: 213 x 19 Name Location Latitude Longitude `Empire State B… `Chicken Tikka` <chr> <chr> <dbl> <dbl> <dbl> <dbl> 1 The … Leicest… 51.5 -0.129 11.2 8.39 2 The … Whiteha… 51.5 -0.127 11.0 8.39 3 Pend… Holborn… 51.5 -0.115 11.0 8.39 4 The … City of… 51.5 -0.0847 11.0 8.39 5 The … Manches… 53.5 -2.35 13.6 9.15 6 The … Windsor 51.5 -0.609 10.6 8.99 7 The … Beacons… 51.6 -0.627 9.75 8.99 8 Good… Tower H… 51.5 -0.0728 11.0 8.39 9 Cast… Manches… 53.5 -2.36 11.5 8.4 10 The … Camden,… 51.5 -0.145 11.2 8.39 # … with 203 more rows, and 13 more variables: `Gammon afternoon deal` <chr>, # `Chocolate Brownie` <dbl>, `Doom Bar` <dbl>, `Birra Moretti` <dbl>, `Porn # Star at 2 x £12` <chr>, `Porn Star (glass)` <chr>, MEAL <dbl>, Notes <chr>, # `Moretti as a % of a tikka` <dbl>, `Moretti as % of burger` <dbl>, `PRICE # BUCKETS` <chr>, `Food cost £` <dbl>, `Drink cost £` <dbl> ``` ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) read_excel("wetherspoon.xlsx") -> *wetherspoon_raw ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) read_excel("wetherspoon.xlsx") -> wetherspoon_raw * # *wetherspoon_raw ``` ]] .column[.content[ ``` # A tibble: 213 x 19 Name Location Latitude Longitude `Empire State B… `Chicken Tikka` <chr> <chr> <dbl> <dbl> <dbl> <dbl> 1 The … Leicest… 51.5 -0.129 11.2 8.39 2 The … Whiteha… 51.5 -0.127 11.0 8.39 3 Pend… Holborn… 51.5 -0.115 11.0 8.39 4 The … City of… 51.5 -0.0847 11.0 8.39 5 The … Manches… 53.5 -2.35 13.6 9.15 6 The … Windsor 51.5 -0.609 10.6 8.99 7 The … Beacons… 51.6 -0.627 9.75 8.99 8 Good… Tower H… 51.5 -0.0728 11.0 8.39 9 Cast… Manches… 53.5 -2.36 11.5 8.4 10 The … Camden,… 51.5 -0.145 11.2 8.39 # … with 203 more rows, and 13 more variables: `Gammon afternoon deal` <chr>, # `Chocolate Brownie` <dbl>, `Doom Bar` <dbl>, `Birra Moretti` <dbl>, `Porn # Star at 2 x £12` <chr>, `Porn Star (glass)` <chr>, MEAL <dbl>, Notes <chr>, # `Moretti as a % of a tikka` <dbl>, `Moretti as % of burger` <dbl>, `PRICE # BUCKETS` <chr>, `Food cost £` <dbl>, `Drink cost £` <dbl> ``` ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) read_excel("wetherspoon.xlsx") -> wetherspoon_raw # wetherspoon_raw %>% * janitor::clean_names() ``` ]] .column[.content[ ``` # A tibble: 213 x 19 name location latitude longitude empire_state_bu… chicken_tikka <chr> <chr> <dbl> <dbl> <dbl> <dbl> 1 The … Leicest… 51.5 -0.129 11.2 8.39 2 The … Whiteha… 51.5 -0.127 11.0 8.39 3 Pend… Holborn… 51.5 -0.115 11.0 8.39 4 The … City of… 51.5 -0.0847 11.0 8.39 5 The … Manches… 53.5 -2.35 13.6 9.15 6 The … Windsor 51.5 -0.609 10.6 8.99 7 The … Beacons… 51.6 -0.627 9.75 8.99 8 Good… Tower H… 51.5 -0.0728 11.0 8.39 9 Cast… Manches… 53.5 -2.36 11.5 8.4 10 The … Camden,… 51.5 -0.145 11.2 8.39 # … with 203 more rows, and 13 more variables: gammon_afternoon_deal <chr>, # chocolate_brownie <dbl>, doom_bar <dbl>, birra_moretti <dbl>, # porn_star_at_2_x_12 <chr>, porn_star_glass <chr>, meal <dbl>, notes <chr>, # moretti_as_a_percent_of_a_tikka <dbl>, moretti_as_percent_of_burger <dbl>, # price_buckets <chr>, food_cost <dbl>, drink_cost <dbl> ``` ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) read_excel("wetherspoon.xlsx") -> wetherspoon_raw # wetherspoon_raw %>% janitor::clean_names() %>% * select(1:5) ``` ]] .column[.content[ ``` # A tibble: 213 x 5 name location latitude longitude empire_state_burg… <chr> <chr> <dbl> <dbl> <dbl> 1 The Moon Under Wat… Leicester Square, … 51.5 -0.129 11.2 2 The Lord Moon of t… Whitehall, London 51.5 -0.127 11.0 3 Pendrel's Oak Holborn, London 51.5 -0.115 11.0 4 The Cross Keyes City of London, Lo… 51.5 -0.0847 11.0 5 The Mardi Gras Manchester 53.5 -2.35 13.6 6 The King and Castle Windsor 51.5 -0.609 10.6 7 The Hope & Champion Beaconsfield Servi… 51.6 -0.627 9.75 8 Goodman's Field Tower Hamlets, Lon… 51.5 -0.0728 11.0 9 Castle In the Air Manchester 53.5 -2.36 11.5 10 The Ice Wharf Camden, London 51.5 -0.145 11.2 # … with 203 more rows ``` ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(rnaturalearth) library(readxl) read_excel("wetherspoon.xlsx") -> wetherspoon_raw # wetherspoon_raw %>% janitor::clean_names() %>% select(1:5) -> *wetherspoon ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r *rnaturalearth::ne_countries( * scale = "medium", returnclass = "sf") ``` ]] .column[.content[ ``` Simple feature collection with 241 features and 63 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: scalerank featurecla labelrank sovereignt sov_a3 adm0_dif 0 3 Admin-0 country 5 Netherlands NL1 1 1 1 Admin-0 country 3 Afghanistan AFG 0 2 1 Admin-0 country 3 Angola AGO 0 3 1 Admin-0 country 6 United Kingdom GB1 1 4 1 Admin-0 country 6 Albania ALB 0 5 3 Admin-0 country 6 Finland FI1 1 6 3 Admin-0 country 6 Andorra AND 0 7 1 Admin-0 country 4 United Arab Emirates ARE 0 8 1 Admin-0 country 2 Argentina ARG 0 9 1 Admin-0 country 6 Armenia ARM 0 level type admin adm0_a3 geou_dif 0 2 Country Aruba ABW 0 1 2 Sovereign country Afghanistan AFG 0 2 2 Sovereign country Angola AGO 0 3 2 Dependency Anguilla AIA 0 4 2 Sovereign country Albania ALB 0 5 2 Country Aland ALD 0 6 2 Sovereign country Andorra AND 0 7 2 Sovereign country United Arab Emirates ARE 0 8 2 Sovereign country Argentina ARG 0 9 2 Sovereign country Armenia ARM 0 geounit gu_a3 su_dif subunit su_a3 brk_diff 0 Aruba ABW 0 Aruba ABW 0 1 Afghanistan AFG 0 Afghanistan AFG 0 2 Angola AGO 0 Angola AGO 0 3 Anguilla AIA 0 Anguilla AIA 0 4 Albania ALB 0 Albania ALB 0 5 Aland ALD 0 Aland ALD 0 6 Andorra AND 0 Andorra AND 0 7 United Arab Emirates ARE 0 United Arab Emirates ARE 0 8 Argentina ARG 0 Argentina ARG 0 9 Armenia ARM 0 Armenia ARM 0 name name_long brk_a3 brk_name 0 Aruba Aruba ABW Aruba 1 Afghanistan Afghanistan AFG Afghanistan 2 Angola Angola AGO Angola 3 Anguilla Anguilla AIA Anguilla 4 Albania Albania ALB Albania 5 Aland Aland Islands ALD Aland 6 Andorra Andorra AND Andorra 7 United Arab Emirates United Arab Emirates ARE United Arab Emirates 8 Argentina Argentina ARG Argentina 9 Armenia Armenia ARM Armenia 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. 6 <NA> And. AND Principality of Andorra <NA> <NA> 7 <NA> U.A.E. AE United Arab Emirates <NA> <NA> 8 <NA> Arg. AR Argentine Republic <NA> <NA> 9 <NA> Arm. ARM Republic of Armenia <NA> <NA> note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9 0 <NA> Aruba <NA> 4 2 2 1 <NA> Afghanistan <NA> 5 6 8 2 <NA> Angola <NA> 3 2 6 3 <NA> Anguilla <NA> 6 6 6 4 <NA> Albania <NA> 1 4 1 5 <NA> Aland <NA> 4 1 4 6 <NA> Andorra <NA> 1 4 1 7 <NA> United Arab Emirates <NA> 2 1 3 8 <NA> Argentina <NA> 3 1 3 9 <NA> Armenia <NA> 3 1 2 mapcolor13 pop_est gdp_md_est pop_year lastcensus gdp_year 0 9 103065 2258.0 NA 2010 NA 1 7 28400000 22270.0 NA 1979 NA 2 1 12799293 110300.0 NA 1970 NA 3 3 14436 108.9 NA NA NA 4 6 3639453 21810.0 NA 2001 NA 5 6 27153 1563.0 NA NA NA 6 8 83888 3660.0 NA 1989 NA 7 3 4798491 184300.0 NA 2010 NA 8 13 40913584 573900.0 NA 2010 NA 9 10 2967004 18770.0 NA 2001 NA economy income_grp wikipedia fips_10 iso_a2 0 6. Developing region 2. High income: nonOECD NA <NA> AW 1 7. Least developed region 5. Low income NA <NA> AF 2 7. Least developed region 3. Upper middle income NA <NA> AO 3 6. Developing region 3. Upper middle income NA <NA> AI 4 6. Developing region 4. Lower middle income NA <NA> AL 5 2. Developed region: nonG7 1. High income: OECD NA <NA> AX 6 2. Developed region: nonG7 2. High income: nonOECD NA <NA> AD 7 6. Developing region 2. High income: nonOECD NA <NA> AE 8 5. Emerging region: G20 3. Upper middle income NA <NA> AR 9 6. Developing region 4. Lower middle income NA <NA> AM iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un 0 ABW 533 533 AW ABW NA ABW ABW NA 1 AFG 004 004 AF AFG NA AFG AFG NA 2 AGO 024 024 AO AGO NA AGO AGO NA 3 AIA 660 660 <NA> <NA> NA AIA AIA NA 4 ALB 008 008 AL ALB NA ALB ALB NA 5 ALA 248 248 <NA> <NA> NA ALA ALD NA 6 AND 020 020 AD ADO NA AND AND NA 7 ARE 784 784 AE ARE NA ARE ARE NA 8 ARG 032 032 AR ARG NA ARG ARG NA 9 ARM 051 051 AM ARM NA ARM ARM NA adm0_a3_wb continent region_un subregion region_wb 0 NA North America Americas Caribbean Latin America & Caribbean 1 NA Asia Asia Southern Asia South Asia 2 NA Africa Africa Middle Africa Sub-Saharan Africa 3 NA North America Americas Caribbean Latin America & Caribbean 4 NA Europe Europe Southern Europe Europe & Central Asia 5 NA Europe Europe Northern Europe Europe & Central Asia 6 NA Europe Europe Southern Europe Europe & Central Asia 7 NA Asia Asia Western Asia Middle East & North Africa 8 NA South America Americas South America Latin America & Caribbean 9 NA Asia Asia Western Asia Europe & Central Asia name_len long_len abbrev_len tiny homepart geometry 0 5 5 5 4 NA MULTIPOLYGON (((-69.89912 1... 1 11 11 4 NA 1 MULTIPOLYGON (((74.89131 37... 2 6 6 4 NA 1 MULTIPOLYGON (((14.19082 -5... 3 8 8 4 NA NA MULTIPOLYGON (((-63.00122 1... 4 7 7 4 NA 1 MULTIPOLYGON (((20.06396 42... 5 5 13 5 5 NA MULTIPOLYGON (((20.61133 60... 6 7 7 4 5 1 MULTIPOLYGON (((1.706055 42... 7 20 20 6 NA 1 MULTIPOLYGON (((53.92783 24... 8 9 9 4 NA 1 MULTIPOLYGON (((-64.54917 -... 9 7 7 4 NA 1 MULTIPOLYGON (((45.55234 40... ``` ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% * select(name, continent, geometry) ``` ]] .column[.content[ ``` Simple feature collection with 241 features and 2 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: name continent geometry 0 Aruba North America MULTIPOLYGON (((-69.89912 1... 1 Afghanistan Asia MULTIPOLYGON (((74.89131 37... 2 Angola Africa MULTIPOLYGON (((14.19082 -5... 3 Anguilla North America MULTIPOLYGON (((-63.00122 1... 4 Albania Europe MULTIPOLYGON (((20.06396 42... 5 Aland Europe MULTIPOLYGON (((20.61133 60... 6 Andorra Europe MULTIPOLYGON (((1.706055 42... 7 United Arab Emirates Asia MULTIPOLYGON (((53.92783 24... 8 Argentina South America MULTIPOLYGON (((-64.54917 -... 9 Armenia Asia MULTIPOLYGON (((45.55234 40... ``` ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% * filter(name == 'United Kingdom') ``` ]] .column[.content[ ``` Simple feature collection with 1 feature and 2 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -8.144824 ymin: 50.02139 xmax: 1.746582 ymax: 60.83188 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs name continent geometry 1 United Kingdom Europe MULTIPOLYGON (((-1.065576 5... ``` ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> *uk ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk * # *ggplot(data = wetherspoon) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_5_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + * ggplot2::geom_sf(data = uk) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_6_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + * geom_point(aes(x = longitude, * y = latitude, * color = empire_state_burger), * alpha = .3) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_7_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + geom_point(aes(x = longitude, y = latitude, color = empire_state_burger), alpha = .3) + * scale_color_viridis_c() ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_8_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + geom_point(aes(x = longitude, y = latitude, color = empire_state_burger), alpha = .3) + scale_color_viridis_c() + * labs(color = "Empire State Burger\nCost (pounds)") ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_9_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + geom_point(aes(x = longitude, y = latitude, color = empire_state_burger), alpha = .3) + scale_color_viridis_c() + labs(color = "Empire State Burger\nCost (pounds)") + * theme_bw() ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_10_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + geom_point(aes(x = longitude, y = latitude, color = empire_state_burger), alpha = .3) + scale_color_viridis_c() + labs(color = "Empire State Burger\nCost (pounds)") + theme_bw() + * theme(plot.title = element_text(hjust = 0.5)) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_11_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, continent, geometry) %>% filter(name == 'United Kingdom') -> uk # ggplot(data = wetherspoon) + ggplot2::geom_sf(data = uk) + geom_point(aes(x = longitude, y = latitude, color = empire_state_burger), alpha = .3) + scale_color_viridis_c() + labs(color = "Empire State Burger\nCost (pounds)") + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + * theme(axis.title = element_blank()) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/wether_auto_12_output-1.png" width="864" /> ]] --- #Example #2 -- The second example will also draw its shapefiles from the rnaturalearth package. We will also use a dataset containing the defense expenditure of NATO member states. We will make a colored map that allows for easy visual comparison of spending levels. This type is called a choropleth map. --- ```r library(readxl) nato_expenditure <- read_excel("nato_expenditure.xlsx") nato_expenditure ``` ``` # A tibble: 29 x 2 name spendingPercap <chr> <dbl> 1 Albania 1.26 2 Belgium 0.93 3 Bulgaria 1.61 4 Canada 1.27 5 Croatia 1.75 6 Czech Republic 1.19 7 Denmark 1.35 8 Estonia 2.13 9 France 1.84 10 Germany 1.36 # … with 19 more rows ``` --- class: split-40 count: false .column[.content[ ```r *nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", * "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") * # *rnaturalearth::ne_countries( * scale = "medium", returnclass = "sf") ``` ]] .column[.content[ ``` Simple feature collection with 241 features and 63 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: scalerank featurecla labelrank sovereignt sov_a3 adm0_dif 0 3 Admin-0 country 5 Netherlands NL1 1 1 1 Admin-0 country 3 Afghanistan AFG 0 2 1 Admin-0 country 3 Angola AGO 0 3 1 Admin-0 country 6 United Kingdom GB1 1 4 1 Admin-0 country 6 Albania ALB 0 5 3 Admin-0 country 6 Finland FI1 1 6 3 Admin-0 country 6 Andorra AND 0 7 1 Admin-0 country 4 United Arab Emirates ARE 0 8 1 Admin-0 country 2 Argentina ARG 0 9 1 Admin-0 country 6 Armenia ARM 0 level type admin adm0_a3 geou_dif 0 2 Country Aruba ABW 0 1 2 Sovereign country Afghanistan AFG 0 2 2 Sovereign country Angola AGO 0 3 2 Dependency Anguilla AIA 0 4 2 Sovereign country Albania ALB 0 5 2 Country Aland ALD 0 6 2 Sovereign country Andorra AND 0 7 2 Sovereign country United Arab Emirates ARE 0 8 2 Sovereign country Argentina ARG 0 9 2 Sovereign country Armenia ARM 0 geounit gu_a3 su_dif subunit su_a3 brk_diff 0 Aruba ABW 0 Aruba ABW 0 1 Afghanistan AFG 0 Afghanistan AFG 0 2 Angola AGO 0 Angola AGO 0 3 Anguilla AIA 0 Anguilla AIA 0 4 Albania ALB 0 Albania ALB 0 5 Aland ALD 0 Aland ALD 0 6 Andorra AND 0 Andorra AND 0 7 United Arab Emirates ARE 0 United Arab Emirates ARE 0 8 Argentina ARG 0 Argentina ARG 0 9 Armenia ARM 0 Armenia ARM 0 name name_long brk_a3 brk_name 0 Aruba Aruba ABW Aruba 1 Afghanistan Afghanistan AFG Afghanistan 2 Angola Angola AGO Angola 3 Anguilla Anguilla AIA Anguilla 4 Albania Albania ALB Albania 5 Aland Aland Islands ALD Aland 6 Andorra Andorra AND Andorra 7 United Arab Emirates United Arab Emirates ARE United Arab Emirates 8 Argentina Argentina ARG Argentina 9 Armenia Armenia ARM Armenia 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. 6 <NA> And. AND Principality of Andorra <NA> <NA> 7 <NA> U.A.E. AE United Arab Emirates <NA> <NA> 8 <NA> Arg. AR Argentine Republic <NA> <NA> 9 <NA> Arm. ARM Republic of Armenia <NA> <NA> note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9 0 <NA> Aruba <NA> 4 2 2 1 <NA> Afghanistan <NA> 5 6 8 2 <NA> Angola <NA> 3 2 6 3 <NA> Anguilla <NA> 6 6 6 4 <NA> Albania <NA> 1 4 1 5 <NA> Aland <NA> 4 1 4 6 <NA> Andorra <NA> 1 4 1 7 <NA> United Arab Emirates <NA> 2 1 3 8 <NA> Argentina <NA> 3 1 3 9 <NA> Armenia <NA> 3 1 2 mapcolor13 pop_est gdp_md_est pop_year lastcensus gdp_year 0 9 103065 2258.0 NA 2010 NA 1 7 28400000 22270.0 NA 1979 NA 2 1 12799293 110300.0 NA 1970 NA 3 3 14436 108.9 NA NA NA 4 6 3639453 21810.0 NA 2001 NA 5 6 27153 1563.0 NA NA NA 6 8 83888 3660.0 NA 1989 NA 7 3 4798491 184300.0 NA 2010 NA 8 13 40913584 573900.0 NA 2010 NA 9 10 2967004 18770.0 NA 2001 NA economy income_grp wikipedia fips_10 iso_a2 0 6. Developing region 2. High income: nonOECD NA <NA> AW 1 7. Least developed region 5. Low income NA <NA> AF 2 7. Least developed region 3. Upper middle income NA <NA> AO 3 6. Developing region 3. Upper middle income NA <NA> AI 4 6. Developing region 4. Lower middle income NA <NA> AL 5 2. Developed region: nonG7 1. High income: OECD NA <NA> AX 6 2. Developed region: nonG7 2. High income: nonOECD NA <NA> AD 7 6. Developing region 2. High income: nonOECD NA <NA> AE 8 5. Emerging region: G20 3. Upper middle income NA <NA> AR 9 6. Developing region 4. Lower middle income NA <NA> AM iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un 0 ABW 533 533 AW ABW NA ABW ABW NA 1 AFG 004 004 AF AFG NA AFG AFG NA 2 AGO 024 024 AO AGO NA AGO AGO NA 3 AIA 660 660 <NA> <NA> NA AIA AIA NA 4 ALB 008 008 AL ALB NA ALB ALB NA 5 ALA 248 248 <NA> <NA> NA ALA ALD NA 6 AND 020 020 AD ADO NA AND AND NA 7 ARE 784 784 AE ARE NA ARE ARE NA 8 ARG 032 032 AR ARG NA ARG ARG NA 9 ARM 051 051 AM ARM NA ARM ARM NA adm0_a3_wb continent region_un subregion region_wb 0 NA North America Americas Caribbean Latin America & Caribbean 1 NA Asia Asia Southern Asia South Asia 2 NA Africa Africa Middle Africa Sub-Saharan Africa 3 NA North America Americas Caribbean Latin America & Caribbean 4 NA Europe Europe Southern Europe Europe & Central Asia 5 NA Europe Europe Northern Europe Europe & Central Asia 6 NA Europe Europe Southern Europe Europe & Central Asia 7 NA Asia Asia Western Asia Middle East & North Africa 8 NA South America Americas South America Latin America & Caribbean 9 NA Asia Asia Western Asia Europe & Central Asia name_len long_len abbrev_len tiny homepart geometry 0 5 5 5 4 NA MULTIPOLYGON (((-69.89912 1... 1 11 11 4 NA 1 MULTIPOLYGON (((74.89131 37... 2 6 6 4 NA 1 MULTIPOLYGON (((14.19082 -5... 3 8 8 4 NA NA MULTIPOLYGON (((-63.00122 1... 4 7 7 4 NA 1 MULTIPOLYGON (((20.06396 42... 5 5 13 5 5 NA MULTIPOLYGON (((20.61133 60... 6 7 7 4 5 1 MULTIPOLYGON (((1.706055 42... 7 20 20 6 NA 1 MULTIPOLYGON (((53.92783 24... 8 9 9 4 NA 1 MULTIPOLYGON (((-64.54917 -... 9 7 7 4 NA 1 MULTIPOLYGON (((45.55234 40... ``` ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% * select(name, pop_est, gdp_md_est, * continent, geometry) ``` ]] .column[.content[ ``` Simple feature collection with 241 features and 4 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: name pop_est gdp_md_est continent 0 Aruba 103065 2258.0 North America 1 Afghanistan 28400000 22270.0 Asia 2 Angola 12799293 110300.0 Africa 3 Anguilla 14436 108.9 North America 4 Albania 3639453 21810.0 Europe 5 Aland 27153 1563.0 Europe 6 Andorra 83888 3660.0 Europe 7 United Arab Emirates 4798491 184300.0 Asia 8 Argentina 40913584 573900.0 South America 9 Armenia 2967004 18770.0 Asia geometry 0 MULTIPOLYGON (((-69.89912 1... 1 MULTIPOLYGON (((74.89131 37... 2 MULTIPOLYGON (((14.19082 -5... 3 MULTIPOLYGON (((-63.00122 1... 4 MULTIPOLYGON (((20.06396 42... 5 MULTIPOLYGON (((20.61133 60... 6 MULTIPOLYGON (((1.706055 42... 7 MULTIPOLYGON (((53.92783 24... 8 MULTIPOLYGON (((-64.54917 -... 9 MULTIPOLYGON (((45.55234 40... ``` ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% * filter(name %in% nato_names) ``` ]] .column[.content[ ``` Simple feature collection with 28 features and 4 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -178.1945 ymin: -21.36904 xmax: 179.78 ymax: 83.11611 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: name pop_est gdp_md_est continent 1 Albania 3639453 21810 Europe 2 Belgium 10414336 389300 Europe 3 Bulgaria 7204687 93750 Europe 4 Canada 33487208 1300000 North America 5 Germany 82329758 2918000 Europe 6 Denmark 5500510 203600 Europe 7 Spain 40525002 1403000 Europe 8 Estonia 1299371 27410 Europe 9 France 64057792 2128000 Europe 10 United Kingdom 62262000 1977704 Europe geometry 1 MULTIPOLYGON (((20.06396 42... 2 MULTIPOLYGON (((4.816016 51... 3 MULTIPOLYGON (((28.58535 43... 4 MULTIPOLYGON (((-59.7876 43... 5 MULTIPOLYGON (((14.19824 53... 6 MULTIPOLYGON (((11.36143 54... 7 MULTIPOLYGON (((-17.88794 2... 8 MULTIPOLYGON (((22.61738 58... 9 MULTIPOLYGON (((55.79736 -2... 10 MULTIPOLYGON (((-1.065576 5... ``` ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% * left_join(nato_expenditure, by = "name") ``` ]] .column[.content[ ``` Simple feature collection with 28 features and 5 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -178.1945 ymin: -21.36904 xmax: 179.78 ymax: 83.11611 epsg (SRID): 4326 proj4string: +proj=longlat +datum=WGS84 +no_defs First 10 features: name pop_est gdp_md_est continent spendingPercap 1 Albania 3639453 21810 Europe 1.26 2 Belgium 10414336 389300 Europe 0.93 3 Bulgaria 7204687 93750 Europe 1.61 4 Canada 33487208 1300000 North America 1.27 5 Germany 82329758 2918000 Europe 1.36 6 Denmark 5500510 203600 Europe 1.35 7 Spain 40525002 1403000 Europe 0.92 8 Estonia 1299371 27410 Europe 2.13 9 France 64057792 2128000 Europe 1.84 10 United Kingdom 62262000 1977704 Europe 2.13 geometry 1 MULTIPOLYGON (((20.06396 42... 2 MULTIPOLYGON (((4.816016 51... 3 MULTIPOLYGON (((28.58535 43... 4 MULTIPOLYGON (((-59.7876 43... 5 MULTIPOLYGON (((14.19824 53... 6 MULTIPOLYGON (((11.36143 54... 7 MULTIPOLYGON (((-17.88794 2... 8 MULTIPOLYGON (((22.61738 58... 9 MULTIPOLYGON (((55.79736 -2... 10 MULTIPOLYGON (((-1.065576 5... ``` ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> * nato_countries ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries * # *ggplot(data = nato_countries) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_7_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + * ggplot2::geom_sf(data = nato_countries) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_8_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + * aes(fill = spendingPercap) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_9_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + * scale_fill_viridis_c(option = "magma") ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_10_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + scale_fill_viridis_c(option = "magma") + * ggplot2::coord_sf(xlim = c(-175, 47.5), * ylim = c(23, 85), * expand = FALSE) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_11_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + scale_fill_viridis_c(option = "magma") + ggplot2::coord_sf(xlim = c(-175, 47.5), ylim = c(23, 85), expand = FALSE) + * labs(title = "Defense Expenditure by Share of GDP (%)", * subtitle = "NATO Member States", * fill = "Share of GDP (%)") ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_12_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + scale_fill_viridis_c(option = "magma") + ggplot2::coord_sf(xlim = c(-175, 47.5), ylim = c(23, 85), expand = FALSE) + labs(title = "Defense Expenditure by Share of GDP (%)", subtitle = "NATO Member States", fill = "Share of GDP (%)") + * theme_bw() ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_13_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + scale_fill_viridis_c(option = "magma") + ggplot2::coord_sf(xlim = c(-175, 47.5), ylim = c(23, 85), expand = FALSE) + labs(title = "Defense Expenditure by Share of GDP (%)", subtitle = "NATO Member States", fill = "Share of GDP (%)") + theme_bw() + * theme(plot.title = * element_text(hjust = 0.5)) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_14_output-1.png" width="864" /> ]] --- class: split-40 count: false .column[.content[ ```r nato_names <- c("Albania", "Belgium", "Bulgaria", "Canada", "Croatia", "Czech Republic", "Denmark", "Estonia", "France", "Germany", "Greece", "Hungary", "Iceland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Turkey", "United Kingdom", "United States") # rnaturalearth::ne_countries( scale = "medium", returnclass = "sf") %>% select(name, pop_est, gdp_md_est, continent, geometry) %>% filter(name %in% nato_names) %>% left_join(nato_expenditure, by = "name") -> nato_countries # ggplot(data = nato_countries) + ggplot2::geom_sf(data = nato_countries) + aes(fill = spendingPercap) + scale_fill_viridis_c(option = "magma") + ggplot2::coord_sf(xlim = c(-175, 47.5), ylim = c(23, 85), expand = FALSE) + labs(title = "Defense Expenditure by Share of GDP (%)", subtitle = "NATO Member States", fill = "Share of GDP (%)") + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + * theme(plot.subtitle = * element_text(hjust = 0.5)) ``` ]] .column[.content[ <img src="geom_sf_files/figure-html/nato_auto_15_output-1.png" width="864" /> ]]