title: “G4D- Asignment No 1(i)” author: “Miriam Kasebele” date: “31 10 2018” output: html_document: df_print: paged
Introduction This is an R Markdown Notebook aimed at learning how to make thematic maps. It is based on the cartography R package developed by Timothée Giraud as well as on code written by Bhaskar Karambelkar.
The G4D Assignment No. 1 comprises two tasks: (i) to reproduce and publish a notebook similar to this one; and (ii) to add new examples of R mapping functionalities to the notebook based on the geospatial data processing skills learned at Data Camp. The assignment can be done using RStudio Cloud (for editing the document) and RPubs (for publishing the document).
Static Mapping
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.3-6, (SVN revision 773)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
## Path to GDAL shared files: /usr/share/gdal/2.1
## GDAL binary built with GEOS: TRUE
## Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
## Path to PROJ.4 shared files: (autodetected)
## Linking to sp version: 1.3-1
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.3, PROJ 4.9.2
library(sp)
library(cartography)
library(rgdal)
### getting vector data
download.file("http://data.biogeo.ucdavis.edu/data/diva/adm/TZA_adm.zip",
destfile = "./data/TZA_adm.zip" , mode='wb')
unzip("./data/TZA_adm.zip", exdir = "./data")
spcol <- readOGR(dsn = "./data/TZA_adm1.shp", verbose = FALSE)
class(spcol)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
spcol@data$COLOUR <- "#FFFFFF"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 0] <- "#006837"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 1] <- "#52BE80"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 2] <- "#28B463"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 3] <- "#D4EFDF"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 4] <- "#138D75"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 5] <- "#76D7C4"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 6] <- "#A3E4D7"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 7] <- "#D0ECE7"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 8] <- "#FAE5D3"
spcol@data$COLOUR[(as.numeric(as.character(spcol@data$ID_1)) %% 10) == 9] <- "#A9CCE3"
plot(spcol, col=spcol$COLOUR, main = "Tanzania's Departments")
library(sf)
sfcol <- st_read(dsn = "./data/TZA_adm1.shp", quiet = TRUE)
class(sfcol)
## [1] "sf" "data.frame"
sfcol$COLOUR <- spcol@data$COLOUR
plot(st_geometry(sfcol), col=sfcol$COLOUR, main = "Tanzania's Departments")
oldw <- getOption("warn")
options(warn = -1)
options(warn = oldw)
library(cartography)
data(nuts2006)
plot(nuts0.spdf, border = NA, col = NA, bg = "#A6CAE0")
# Plot non european space
plot(world.spdf, col = "#E3DEBF", border=NA, add=TRUE)
# Plot a layer of countries borders
plot(nuts0.spdf, border = "grey20", lwd = 3, add = TRUE)
# Plot a layer of NUTS1
plot(nuts1.spdf, border = "grey30", lwd = 2, add = TRUE)
# Plot a layer of NUTS2
plot(nuts2.spdf, border = "grey40", lwd = 0.5, add = TRUE)
# Plot a layer of NUTS3
plot(nuts3.spdf, border = "grey20", lwd = 0.1, add = TRUE)
# Layout plot
layoutLayer(title = "Most Populated Countries in Europe", # title of the map
author = "Author:Miriam Kasebele", #
sources = "Sources: Eurostat", #
scale = NULL, # no scale
col = NA, # no color for the title box
coltitle = "black", # color of the title
frame = TRUE,
bg = "#A6CAE0", # background of the map
extent = nuts0.spdf) # set the extent of the map
# Non European space
plot(world.spdf, col = "#AAB7B8", border = NA, add = TRUE)
# European (EU28) countries
plot(nuts0.spdf, col = "#F12BD0",border = "white", lwd = 1, add = TRUE)
# Selection of the 10 most populated countries of Europe
dflab <- nuts0.df[order(nuts0.df$pop2008, decreasing = TRUE),][1:10,]
# Label creation
dflab$lab <- paste(dflab$id, "\n", round(dflab$pop2008/1000000,0), "M", sep ="")
# Label plot of the 10 most populated countries
labelLayer(spdf = nuts0.spdf, # SpatialPolygonsDataFrame used to plot he labels
df = dflab, # data frame containing the lables
txt = "lab", # label field in df
col = "#690409", # color of the labels
cex = 0.6, # size of the labels
font = 2) # label font
# Add an explanation text
text(x = 5477360, y = 4177311, labels = "The 10 most populated countries of Europe
Total population 2008 [millions]", cex = 0.7, adj = 0)
# Compute the compound annual growth rate
nuts2.df$cagr <- (((nuts2.df$pop2008 / nuts2.df$pop1999)^(1/9)) - 1) * 100
# Set a custom color palette
cols <- carto.pal(pal1 = "green.pal", # first color gradient
n1 = 2, # number of colors in the first gradiant
pal2 = "red.pal", # second color gradient
n2 = 4) # number of colors in the second gradiant
# Plot a layer with the extent of the EU28 countries with only a background color
plot(nuts0.spdf, border = NA, col = NA, bg = "#A6CAE0")
# Plot non european space
plot(world.spdf, col = "#E3DEBF", border=NA, add=TRUE)
# Plot the compound annual growth rate
choroLayer(spdf = nuts2.spdf, # SpatialPolygonsDataFrame of the regions
df = nuts2.df, # data frame with compound annual growth rate
var = "cagr", # compound annual growth rate field in df
breaks = c(-2.43,-1,0,0.5,1,2,3.1), # list of breaks
col = cols, # colors
border = "grey40", # color of the polygons borders
lwd = 0.5, # width of the borders
legend.pos = "right", # position of the legend
legend.title.txt = "Compound Annual\nGrowth Rate", # title of the legend
legend.values.rnd = 2, # number of decimal in the legend values
add = TRUE) # add the layer to the current plot
# Plot a layer of countries borders
plot(nuts0.spdf,border = "grey20", lwd=0.75, add=TRUE)
# Layout plot
layoutLayer(title = "Demographic Trends", author = "cartography",
sources = "Eurostat, 2008", frame = TRUE, col = NA,
scale = NULL,coltitle = "black",
south = TRUE) # add a south arrow
##Interactive Mapping
library(ggplot2)
library(ggiraph)
library(rnaturalearth)
library(readr)
library(RCurl)
## Loading required package: bitops
###Loading required bitops
urlfile <- "https://raw.github.com/bhaskarvk/user2017.geodataviz/master/inst/extdata/africa-internet_usage-2015.csv"
internet_usage <- read.csv(urlfile)
head(internet_usage)
## Country.Name Country.Code
## 1 Algeria DZA
## 2 Angola AGO
## 3 Bahrain BHR
## 4 Benin BEN
## 5 Botswana BWA
## 6 Burkina Faso BFA
## Series.Name Series.Code
## 1 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 2 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 3 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 4 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 5 Individuals using the Internet (% of population) IT.NET.USER.ZS
## 6 Individuals using the Internet (% of population) IT.NET.USER.ZS
## X2014..YR2014. X2015..YR2015. X2016..YR2016.
## 1 25.00000 38.200000 ..
## 2 10.20000 12.400000 ..
## 3 90.50313 93.478301 ..
## 4 6.00000 6.787703 ..
## 5 18.50000 27.500000 ..
## 6 9.40000 11.387646 ..
names(internet_usage) <- c("Country Name", "Country Code", "Series Name", "Series Code",
"2014 [YR2014]", "2015 [YR2015]", "2015 [YR20156]")
names(internet_usage)
## [1] "Country Name" "Country Code" "Series Name" "Series Code"
## [5] "2014 [YR2014]" "2015 [YR2015]" "2015 [YR20156]"
world <- sf::st_as_sf(rnaturalearth::countries110)
##str(world)
length(unique(world$iso_a3))
## [1] 175
print(world)
## Simple feature collection with 177 features and 63 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## First 10 features:
## scalerank featurecla labelrank sovereignt sov_a3 adm0_dif
## 0 1 Admin-0 country 3 Afghanistan AFG 0
## 1 1 Admin-0 country 3 Angola AGO 0
## 2 1 Admin-0 country 6 Albania ALB 0
## 3 1 Admin-0 country 4 United Arab Emirates ARE 0
## 4 1 Admin-0 country 2 Argentina ARG 0
## 5 1 Admin-0 country 6 Armenia ARM 0
## 6 1 Admin-0 country 4 Antarctica ATA 0
## 7 3 Admin-0 country 6 France FR1 1
## 8 1 Admin-0 country 2 Australia AU1 1
## 9 1 Admin-0 country 4 Austria AUT 0
## level type admin adm0_a3
## 0 2 Sovereign country Afghanistan AFG
## 1 2 Sovereign country Angola AGO
## 2 2 Sovereign country Albania ALB
## 3 2 Sovereign country United Arab Emirates ARE
## 4 2 Sovereign country Argentina ARG
## 5 2 Sovereign country Armenia ARM
## 6 2 Indeterminate Antarctica ATA
## 7 2 Dependency French Southern and Antarctic Lands ATF
## 8 2 Country Australia AUS
## 9 2 Sovereign country Austria AUT
## geou_dif geounit gu_a3 su_dif
## 0 0 Afghanistan AFG 0
## 1 0 Angola AGO 0
## 2 0 Albania ALB 0
## 3 0 United Arab Emirates ARE 0
## 4 0 Argentina ARG 0
## 5 0 Armenia ARM 0
## 6 0 Antarctica ATA 0
## 7 0 French Southern and Antarctic Lands ATF 0
## 8 0 Australia AUS 0
## 9 0 Austria AUT 0
## subunit su_a3 brk_diff
## 0 Afghanistan AFG 0
## 1 Angola AGO 0
## 2 Albania ALB 0
## 3 United Arab Emirates ARE 0
## 4 Argentina ARG 0
## 5 Armenia ARM 0
## 6 Antarctica ATA 0
## 7 French Southern and Antarctic Lands ATF 0
## 8 Australia AUS 0
## 9 Austria AUT 0
## name name_long brk_a3
## 0 Afghanistan Afghanistan AFG
## 1 Angola Angola AGO
## 2 Albania Albania ALB
## 3 United Arab Emirates United Arab Emirates ARE
## 4 Argentina Argentina ARG
## 5 Armenia Armenia ARM
## 6 Antarctica Antarctica ATA
## 7 Fr. S. Antarctic Lands French Southern and Antarctic Lands ATF
## 8 Australia Australia AUS
## 9 Austria Austria AUT
## brk_name brk_group abbrev postal
## 0 Afghanistan <NA> Afg. AF
## 1 Angola <NA> Ang. AO
## 2 Albania <NA> Alb. AL
## 3 United Arab Emirates <NA> U.A.E. AE
## 4 Argentina <NA> Arg. AR
## 5 Armenia <NA> Arm. ARM
## 6 Antarctica <NA> Ant. AQ
## 7 Fr. S. and Antarctic Lands <NA> Fr. S.A.L. TF
## 8 Australia <NA> Auz. AU
## 9 Austria <NA> Aust. A
## formal_en formal_fr note_adm0
## 0 Islamic State of Afghanistan <NA> <NA>
## 1 People's Republic of Angola <NA> <NA>
## 2 Republic of Albania <NA> <NA>
## 3 United Arab Emirates <NA> <NA>
## 4 Argentine Republic <NA> <NA>
## 5 Republic of Armenia <NA> <NA>
## 6 <NA> <NA> <NA>
## 7 Territory of the French Southern and Antarctic Lands <NA> Fr.
## 8 Commonwealth of Australia <NA> <NA>
## 9 Republic of Austria <NA> <NA>
## note_brk name_sort
## 0 <NA> Afghanistan
## 1 <NA> Angola
## 2 <NA> Albania
## 3 <NA> United Arab Emirates
## 4 <NA> Argentina
## 5 <NA> Armenia
## 6 Multiple claims held in abeyance Antarctica
## 7 <NA> French Southern and Antarctic Lands
## 8 <NA> Australia
## 9 <NA> Austria
## name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13 pop_est gdp_md_est
## 0 <NA> 5 6 8 7 28400000 22270.0
## 1 <NA> 3 2 6 1 12799293 110300.0
## 2 <NA> 1 4 1 6 3639453 21810.0
## 3 <NA> 2 1 3 3 4798491 184300.0
## 4 <NA> 3 1 3 13 40913584 573900.0
## 5 <NA> 3 1 2 10 2967004 18770.0
## 6 <NA> 4 5 1 NA 3802 760.4
## 7 <NA> 7 5 9 11 140 16.0
## 8 <NA> 1 2 2 7 21262641 800200.0
## 9 <NA> 3 1 3 4 8210281 329500.0
## pop_year lastcensus gdp_year economy
## 0 NA 1979 NA 7. Least developed region
## 1 NA 1970 NA 7. Least developed region
## 2 NA 2001 NA 6. Developing region
## 3 NA 2010 NA 6. Developing region
## 4 NA 2010 NA 5. Emerging region: G20
## 5 NA 2001 NA 6. Developing region
## 6 NA NA NA 6. Developing region
## 7 NA NA NA 6. Developing region
## 8 NA 2006 NA 2. Developed region: nonG7
## 9 NA 2011 NA 2. Developed region: nonG7
## income_grp wikipedia fips_10 iso_a2 iso_a3 iso_n3 un_a3
## 0 5. Low income NA <NA> AF AFG 004 004
## 1 3. Upper middle income NA <NA> AO AGO 024 024
## 2 4. Lower middle income NA <NA> AL ALB 008 008
## 3 2. High income: nonOECD NA <NA> AE ARE 784 784
## 4 3. Upper middle income NA <NA> AR ARG 032 032
## 5 4. Lower middle income NA <NA> AM ARM 051 051
## 6 2. High income: nonOECD NA <NA> AQ ATA 010 <NA>
## 7 2. High income: nonOECD NA <NA> TF ATF 260 <NA>
## 8 1. High income: OECD NA <NA> AU AUS 036 036
## 9 1. High income: OECD NA <NA> AT AUT 040 040
## wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb
## 0 AF AFG NA AFG AFG NA NA
## 1 AO AGO NA AGO AGO NA NA
## 2 AL ALB NA ALB ALB NA NA
## 3 AE ARE NA ARE ARE NA NA
## 4 AR ARG NA ARG ARG NA NA
## 5 AM ARM NA ARM ARM NA NA
## 6 <NA> <NA> NA ATA ATA NA NA
## 7 <NA> <NA> NA ATF ATF NA NA
## 8 AU AUS NA AUS AUS NA NA
## 9 AT AUT NA AUT AUT NA NA
## continent region_un
## 0 Asia Asia
## 1 Africa Africa
## 2 Europe Europe
## 3 Asia Asia
## 4 South America Americas
## 5 Asia Asia
## 6 Antarctica Antarctica
## 7 Seven seas (open ocean) Seven seas (open ocean)
## 8 Oceania Oceania
## 9 Europe Europe
## subregion region_wb name_len long_len
## 0 Southern Asia South Asia 11 11
## 1 Middle Africa Sub-Saharan Africa 6 6
## 2 Southern Europe Europe & Central Asia 7 7
## 3 Western Asia Middle East & North Africa 20 20
## 4 South America Latin America & Caribbean 9 9
## 5 Western Asia Europe & Central Asia 7 7
## 6 Antarctica Antarctica 10 10
## 7 Seven seas (open ocean) Sub-Saharan Africa 22 35
## 8 Australia and New Zealand East Asia & Pacific 9 9
## 9 Western Europe Europe & Central Asia 7 7
## abbrev_len tiny homepart geometry
## 0 4 NA 1 MULTIPOLYGON (((61.21082 35...
## 1 4 NA 1 MULTIPOLYGON (((16.32653 -5...
## 2 4 NA 1 MULTIPOLYGON (((20.59025 41...
## 3 6 NA 1 MULTIPOLYGON (((51.57952 24...
## 4 4 NA 1 MULTIPOLYGON (((-65.5 -55.2...
## 5 4 NA 1 MULTIPOLYGON (((43.58275 41...
## 6 4 NA 1 MULTIPOLYGON (((-59.57209 -...
## 7 10 2 NA MULTIPOLYGON (((68.935 -48....
## 8 4 NA 1 MULTIPOLYGON (((145.398 -40...
## 9 5 NA 1 MULTIPOLYGON (((16.97967 48...
africa <- dplyr::filter(world, region_un=='Africa') %>%
dplyr::left_join(internet_usage %>% dplyr::select(
`Country Code`, `2015 [YR2015]`
) %>% dplyr::rename(iso_a3=`Country Code`, internet.usage.2015=`2015 [YR2015]`),
by = 'iso_a3') %>%
st_transform(crs="+proj=laea +lon_0=18.984375")
## Warning: Column `iso_a3` joining character vector and factor, coercing into
## character vector
africa$internet.usage.2015
## [1] 12.400000 4.866224 6.787703 11.387646 27.500000 4.563264 21.000000
## [8] 20.680148 3.800000 7.615975 11.922431 38.200000 37.819383 1.083733
## [15] 11.600000 23.500000 23.478128 4.700000 17.119821 3.540707 21.320000
## [22] 45.622801 5.903868 19.016080 16.071708 57.080000 4.173972 10.336925
## [29] 9.000000 15.199127 9.298148 22.307015 2.220165 47.442550 18.000000
## [36] NA 26.614929 17.929787 21.690264 2.500000 NA 1.760000
## [43] 30.382701 2.700000 7.120000 48.519836 5.355144 19.221100 51.919116
## [50] 21.000000 16.360000
africa.centers <- st_centroid(africa)
africa.spdf <- methods::as(africa, 'Spatial')
africa.spdf@data$id <- row.names(africa.spdf@data)
africa.tidy <- broom::tidy(africa.spdf)
library(colormap)
library(widgetframe)
## Loading required package: htmlwidgets
africa.tidy <- dplyr::left_join(africa.tidy, africa.spdf@data, by='id')
g <- ggplot(africa.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=internet.usage.2015,
tooltip=sprintf("%s<br/>%s",iso_a3,internet.usage.2015))) +
hrbrthemes::theme_ipsum() +
colormap::scale_fill_colormap(
colormap=colormap::colormaps$copper, reverse = T) +
labs(title='Internet Usage in Africa in 2015', subtitle='As Percent of Population',
caption='Source: World Bank Open Data.')
g