Quick Demo for the SEA course: GIS & R
setwd("~/Desktop/Sustainability_Development/Sustainability_Development")
#install.packages(c("ggplot2", "sf", "rnaturalearth", "rnaturalearthdata", "dplyer", "rgeos", "leaflet"))
library(ggplot2)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(rnaturalearth)
library(rnaturalearthdata)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(rgeos)
## Loading required package: sp
## rgeos version: 0.5-7, (SVN revision 676)
## GEOS runtime version: 3.8.0-CAPI-1.13.1
## Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
## Linking to sp version: 1.4-5
## Polygon checking: TRUE
library(leaflet)
world <- ne_countries(scale ="medium", returnclass="sf")
class(world)
## [1] "sf" "data.frame"
ggplot(data = world) +
geom_sf() +
ggtitle("World Map", subtitle="R Natural Earth Data")
ggplot(world) +
geom_sf(aes(fill =pop_est)) +
labs(fill ="Population size")
ggplot(world) +
geom_sf(aes(fill =pop_est)) +
scale_fill_viridis_c(option ="plasma", trans ="sqrt")
labs(fill ="Population size")
## $fill
## [1] "Population size"
##
## attr(,"class")
## [1] "labels"
WP = read.csv('https://raw.githubusercontent.com/lindangulopez/Sustainability_Development/main/GIS/worldPovertyRaw.csv?token=AKMB7ES2XU7ENSY2IOR6KPTBIDI5I')
head(WP)
## Country X...1.90.8..1. X...3.20.6. X...5.50.7. Year Continent X X.1
## 1 Albania 1.30% 8.20% 33.80% 2017 Europe NA NA
## 2 Algeria 0.40% 3.70% 28.60% 2011 Africa NA NA
## 3 Angola 49.90% 72.00% 89.30% 2018 Africa NA NA
## 4 Argentina 1.50% 5.00% 14.00% 2019 South America NA NA
## 5 Armenia 1.10% 10.00% 44.00% 2019 Asia NA NA
## 6 Australia 0.50% 0.70% 0.70% 2014 Oceania NA NA
## X.2 X.3 X.4
## 1 NA NA NA
## 2 NA NA NA
## 3 NA NA NA
## 4 NA NA NA
## 5 NA NA NA
## 6 NA NA NA
## X.5
## 1
## 2
## 3 https://en.wikipedia.org/wiki/List_of_countries_by_percentage_of_population_living_in_poverty
## 4
## 5 Percent of population living on less than $1.90, $3.20 and $5.50 a day
## 6
## Did not work for me
#WP$LessThan1.90 <-as.numeric(WP$LessThan1.90)
#WP$LessThan3.20 <-as.numeric(WP$LessThan3.20)
#WP$LessThan5.50 <-as.numeric(WP$LessThan5.50)
#...
## Used cleaned Data Set
WP = read.csv('https://raw.githubusercontent.com/lindangulopez/Sustainability_Development/main/GIS/WorldPoverty.csv?token=AKMB7EQAD5JGBLFIHETVRYDBIC75O')
head(WP)
## World_ID Country LessThan1.90 LessThan3.20
## 1 Afghanistan Afghanistan N/A N/A
## 2 Albania Albania 1.3 8.2
## 3 Algeria Algeria 0.4 3.7
## 4 Andorra Andorra N/A N/A
## 5 Angola Angola 51.8 73.2
## 6 Antigua and Barbuda Antigua and Barbuda N/A N/A
## LessThan5.50 Year Continent X X.1
## 1 N/A N/A Asia NA
## 2 33.8 2017 Europe NA
## 3 28.6 2011 Africa NA
## 4 N/A N/A Europe NA
## 5 89.3 2018 Africa NA
## 6 N/A N/A North America NA
## rerun just in case
WP$LessThan1.90 <-as.numeric(WP$LessThan1.90)
WP$LessThan3.20 <-as.numeric(WP$LessThan3.20)
WP$LessThan5.50 <-as.numeric(WP$LessThan5.50)
world_WP <-full_join(world, WP, by =c("admin" = "World_ID"))
names(world_WP)
## [1] "scalerank" "featurecla" "labelrank" "sovereignt" "sov_a3"
## [6] "adm0_dif" "level" "type" "admin" "adm0_a3"
## [11] "geou_dif" "geounit" "gu_a3" "su_dif" "subunit"
## [16] "su_a3" "brk_diff" "name" "name_long" "brk_a3"
## [21] "brk_name" "brk_group" "abbrev" "postal" "formal_en"
## [26] "formal_fr" "note_adm0" "note_brk" "name_sort" "name_alt"
## [31] "mapcolor7" "mapcolor8" "mapcolor9" "mapcolor13" "pop_est"
## [36] "gdp_md_est" "pop_year" "lastcensus" "gdp_year" "economy"
## [41] "income_grp" "wikipedia" "fips_10" "iso_a2" "iso_a3"
## [46] "iso_n3" "un_a3" "wb_a2" "wb_a3" "woe_id"
## [51] "adm0_a3_is" "adm0_a3_us" "adm0_a3_un" "adm0_a3_wb" "continent"
## [56] "region_un" "subregion" "region_wb" "name_len" "long_len"
## [61] "abbrev_len" "tiny" "homepart" "Country" "LessThan1.90"
## [66] "LessThan3.20" "LessThan5.50" "Year" "Continent" "X"
## [71] "X.1" "geometry"
head(world_WP)
## Simple feature collection with 6 features and 71 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -70.06611 ymin: -18.01973 xmax: 74.89131 ymax: 60.40581
## CRS: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## scalerank featurecla labelrank sovereignt sov_a3 adm0_dif level
## 1 3 Admin-0 country 5 Netherlands NL1 1 2
## 2 1 Admin-0 country 3 Afghanistan AFG 0 2
## 3 1 Admin-0 country 3 Angola AGO 0 2
## 4 1 Admin-0 country 6 United Kingdom GB1 1 2
## 5 1 Admin-0 country 6 Albania ALB 0 2
## 6 3 Admin-0 country 6 Finland FI1 1 2
## type admin adm0_a3 geou_dif geounit gu_a3 su_dif
## 1 Country Aruba ABW 0 Aruba ABW 0
## 2 Sovereign country Afghanistan AFG 0 Afghanistan AFG 0
## 3 Sovereign country Angola AGO 0 Angola AGO 0
## 4 Dependency Anguilla AIA 0 Anguilla AIA 0
## 5 Sovereign country Albania ALB 0 Albania ALB 0
## 6 Country Aland ALD 0 Aland ALD 0
## subunit su_a3 brk_diff name name_long brk_a3 brk_name
## 1 Aruba ABW 0 Aruba Aruba ABW Aruba
## 2 Afghanistan AFG 0 Afghanistan Afghanistan AFG Afghanistan
## 3 Angola AGO 0 Angola Angola AGO Angola
## 4 Anguilla AIA 0 Anguilla Anguilla AIA Anguilla
## 5 Albania ALB 0 Albania Albania ALB Albania
## 6 Aland ALD 0 Aland Aland Islands ALD Aland
## brk_group abbrev postal formal_en formal_fr note_adm0
## 1 <NA> Aruba AW Aruba <NA> Neth.
## 2 <NA> Afg. AF Islamic State of Afghanistan <NA> <NA>
## 3 <NA> Ang. AO People's Republic of Angola <NA> <NA>
## 4 <NA> Ang. AI <NA> <NA> U.K.
## 5 <NA> Alb. AL Republic of Albania <NA> <NA>
## 6 <NA> Aland AI Ã…land Islands <NA> Fin.
## note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13
## 1 <NA> Aruba <NA> 4 2 2 9
## 2 <NA> Afghanistan <NA> 5 6 8 7
## 3 <NA> Angola <NA> 3 2 6 1
## 4 <NA> Anguilla <NA> 6 6 6 3
## 5 <NA> Albania <NA> 1 4 1 6
## 6 <NA> Aland <NA> 4 1 4 6
## pop_est gdp_md_est pop_year lastcensus gdp_year economy
## 1 103065 2258.0 NA 2010 NA 6. Developing region
## 2 28400000 22270.0 NA 1979 NA 7. Least developed region
## 3 12799293 110300.0 NA 1970 NA 7. Least developed region
## 4 14436 108.9 NA NA NA 6. Developing region
## 5 3639453 21810.0 NA 2001 NA 6. Developing region
## 6 27153 1563.0 NA NA NA 2. Developed region: nonG7
## income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3 wb_a2
## 1 2. High income: nonOECD NA <NA> AW ABW 533 533 AW
## 2 5. Low income NA <NA> AF AFG 004 004 AF
## 3 3. Upper middle income NA <NA> AO AGO 024 024 AO
## 4 3. Upper middle income NA <NA> AI AIA 660 660 <NA>
## 5 4. Lower middle income NA <NA> AL ALB 008 008 AL
## 6 1. High income: OECD NA <NA> AX ALA 248 248 <NA>
## wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb continent
## 1 ABW NA ABW ABW NA NA North America
## 2 AFG NA AFG AFG NA NA Asia
## 3 AGO NA AGO AGO NA NA Africa
## 4 <NA> NA AIA AIA NA NA North America
## 5 ALB NA ALB ALB NA NA Europe
## 6 <NA> NA ALA ALD NA NA Europe
## region_un subregion region_wb name_len long_len
## 1 Americas Caribbean Latin America & Caribbean 5 5
## 2 Asia Southern Asia South Asia 11 11
## 3 Africa Middle Africa Sub-Saharan Africa 6 6
## 4 Americas Caribbean Latin America & Caribbean 8 8
## 5 Europe Southern Europe Europe & Central Asia 7 7
## 6 Europe Northern Europe Europe & Central Asia 5 13
## abbrev_len tiny homepart Country LessThan1.90 LessThan3.20 LessThan5.50
## 1 5 4 NA <NA> NA NA NA
## 2 4 NA 1 Afghanistan 95 116 134
## 3 4 NA 1 Angola 78 90 114
## 4 4 NA NA <NA> NA NA NA
## 5 4 NA 1 Albania 14 103 52
## 6 5 5 NA <NA> NA NA NA
## Year Continent X X.1 geometry
## 1 <NA> <NA> NA <NA> MULTIPOLYGON (((-69.89912 1...
## 2 N/A Asia NA MULTIPOLYGON (((74.89131 37...
## 3 2018 Africa NA MULTIPOLYGON (((14.19082 -5...
## 4 <NA> <NA> NA <NA> MULTIPOLYGON (((-63.00122 1...
## 5 2017 Europe NA MULTIPOLYGON (((20.06396 42...
## 6 <NA> <NA> NA <NA> MULTIPOLYGON (((20.61133 60...
ggplot(world_WP) +
geom_sf(aes(fill = LessThan1.90)) +
scale_fill_viridis_c(option ="plasma", trans ="sqrt")
labs(fill ="Population size")
## $fill
## [1] "Population size"
##
## attr(,"class")
## [1] "labels"
##nonsense plot
world_WP <- st_transform(world_WP, crs = 4326)
world_WP[0,]
## Simple feature collection with 0 features and 71 fields
## Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA
## Geodetic CRS: WGS 84
## [1] scalerank featurecla labelrank sovereignt sov_a3
## [6] adm0_dif level type admin adm0_a3
## [11] geou_dif geounit gu_a3 su_dif subunit
## [16] su_a3 brk_diff name name_long brk_a3
## [21] brk_name brk_group abbrev postal formal_en
## [26] formal_fr note_adm0 note_brk name_sort name_alt
## [31] mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est
## [36] gdp_md_est pop_year lastcensus gdp_year economy
## [41] income_grp wikipedia fips_10 iso_a2 iso_a3
## [46] iso_n3 un_a3 wb_a2 wb_a3 woe_id
## [51] adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb continent
## [56] region_un subregion region_wb name_len long_len
## [61] abbrev_len tiny homepart Country LessThan1.90
## [66] LessThan3.20 LessThan5.50 Year Continent X
## [71] X.1 geometry
## <0 rows> (or 0-length row.names)
leaflet(world_WP) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("Reds",LessThan1.90)(LessThan1.90), highlightOptions = highlightOptions(color ="white", weight = 2, bringToFront = TRUE), popup = paste("Population living with less than $1.90 a day:", (world_WP$LessThan1.90), "%"))
## Zoom in to see popups
leaflet(world_WP) %>%
addTiles%>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5, opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("Reds",LessThan1.90)(LessThan1.90), highlightOptions = highlightOptions(color ="white", weight = 2, bringToFront = TRUE), popup = paste("Population living with less than $1.90 a day:", (world_WP$LessThan1.90), "%"))
You can also embed plots, by setting echo=FALSE:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.