Packages

pacman::p_load(sf,
               rnaturalearth,
               mapview,
               tmap,
               # rnaturalearthhires, 
               rgeos, 
               ggplot2,
               dplyr,
               tidyr,
               rvest,
               coronavirus)
devtools::install_github("ropensci/rnaturalearthhires")
## Skipping install of 'rnaturalearthhires' from a github remote, the SHA1 (2ed7a937) has not changed since last install.
##   Use `force = TRUE` to force installation

Introduction

Polygons

plot(x = 1, 
     y = 1,
     ylab = "",
     xlab = "",
     main = "Point")

plot(x = c(1, 2, 3), y = c(1, 4, 2), type = "l")

plot(x = c(1, 2, 4, 3, 1), y = c(1, 4, 3, 1, 1), type = "l")

labels + polygons = map boarders

labels + polygons + data = choropleth map

World

world <- ne_countries(type = 'countries', scale = 'small', returnclass = "sf")
str(world)
## Classes 'sf' and 'data.frame':   177 obs. of  64 variables:
##  $ scalerank : int  1 1 1 1 1 1 1 3 1 1 ...
##  $ featurecla: chr  "Admin-0 country" "Admin-0 country" "Admin-0 country" "Admin-0 country" ...
##  $ labelrank : num  3 3 6 4 2 6 4 6 2 4 ...
##  $ sovereignt: chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ sov_a3    : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ adm0_dif  : num  0 0 0 0 0 0 0 1 1 0 ...
##  $ level     : num  2 2 2 2 2 2 2 2 2 2 ...
##  $ type      : chr  "Sovereign country" "Sovereign country" "Sovereign country" "Sovereign country" ...
##  $ admin     : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ adm0_a3   : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ geou_dif  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ geounit   : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ gu_a3     : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ su_dif    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ subunit   : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ su_a3     : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ brk_diff  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ name      : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ name_long : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ brk_a3    : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ brk_name  : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ brk_group : chr  NA NA NA NA ...
##  $ abbrev    : chr  "Afg." "Ang." "Alb." "U.A.E." ...
##  $ postal    : chr  "AF" "AO" "AL" "AE" ...
##  $ formal_en : chr  "Islamic State of Afghanistan" "People's Republic of Angola" "Republic of Albania" "United Arab Emirates" ...
##  $ formal_fr : chr  NA NA NA NA ...
##  $ note_adm0 : chr  NA NA NA NA ...
##  $ note_brk  : chr  NA NA NA NA ...
##  $ name_sort : chr  "Afghanistan" "Angola" "Albania" "United Arab Emirates" ...
##  $ name_alt  : chr  NA NA NA NA ...
##  $ mapcolor7 : num  5 3 1 2 3 3 4 7 1 3 ...
##  $ mapcolor8 : num  6 2 4 1 1 1 5 5 2 1 ...
##  $ mapcolor9 : num  8 6 1 3 3 2 1 9 2 3 ...
##  $ mapcolor13: num  7 1 6 3 13 10 NA 11 7 4 ...
##  $ pop_est   : num  28400000 12799293 3639453 4798491 40913584 ...
##  $ gdp_md_est: num  22270 110300 21810 184300 573900 ...
##  $ pop_year  : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ lastcensus: num  1979 1970 2001 2010 2010 ...
##  $ gdp_year  : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ economy   : chr  "7. Least developed region" "7. Least developed region" "6. Developing region" "6. Developing region" ...
##  $ income_grp: chr  "5. Low income" "3. Upper middle income" "4. Lower middle income" "2. High income: nonOECD" ...
##  $ wikipedia : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ fips_10   : chr  NA NA NA NA ...
##  $ iso_a2    : chr  "AF" "AO" "AL" "AE" ...
##  $ iso_a3    : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ iso_n3    : chr  "004" "024" "008" "784" ...
##  $ un_a3     : chr  "004" "024" "008" "784" ...
##  $ wb_a2     : chr  "AF" "AO" "AL" "AE" ...
##  $ wb_a3     : chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ woe_id    : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ adm0_a3_is: chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ adm0_a3_us: chr  "AFG" "AGO" "ALB" "ARE" ...
##  $ adm0_a3_un: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ adm0_a3_wb: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ continent : chr  "Asia" "Africa" "Europe" "Asia" ...
##  $ region_un : chr  "Asia" "Africa" "Europe" "Asia" ...
##  $ subregion : chr  "Southern Asia" "Middle Africa" "Southern Europe" "Western Asia" ...
##  $ region_wb : chr  "South Asia" "Sub-Saharan Africa" "Europe & Central Asia" "Middle East & North Africa" ...
##  $ name_len  : num  11 6 7 20 9 7 10 22 9 7 ...
##  $ long_len  : num  11 6 7 20 9 7 10 35 9 7 ...
##  $ abbrev_len: num  4 4 4 6 4 4 4 10 4 5 ...
##  $ tiny      : num  NA NA NA NA NA NA NA 2 NA NA ...
##  $ homepart  : num  1 1 1 1 1 1 1 NA 1 1 ...
##  $ geometry  :sfc_MULTIPOLYGON of length 177; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:69, 1:2] 61.2 62.2 63 63.2 64 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:63] "scalerank" "featurecla" "labelrank" "sovereignt" ...
head(world)
plot(world$geometry)

africa <- world %>% 
  filter(continent == "Africa")
plot(africa$geometry)

w_africa <- world %>% 
  filter(subregion == "Western Africa")
plot(
  w_africa$geometry,
  col = sf.colors(12, categorical = TRUE),
  border = 'grey',
  axes = TRUE,
  main = "Western Africa"
)

nigeria <- ne_states(country = "Nigeria", returnclass = "sf")

head(nigeria)
plot(nigeria$geometry, col = sf.colors(37, categorical = TRUE), border = 'grey', 
     axes = TRUE, main = "Nigeria")

Scrapping data

url <- "https://en.wikipedia.org/wiki/List_of_Nigerian_states_by_population"

page <- read_html(url)
tables <- html_node(page, ".wikitable")
pop_table <- html_table(tables, fill = TRUE) %>%
  select(state_temp = State, pop_2006_temp = `Population (2006)`, pop_2016_temp = `Population (2016)`)


pop_table$pop_2006 <- as.numeric(gsub(x = pop_table$pop_2006_temp,pattern = ",", replacement = ""))
pop_table$pop_2016 <- as.numeric(gsub(x = pop_table$pop_2016_temp,pattern = ",", replacement = ""))
pop_table$state <- gsub(x = pop_table$state_temp,pattern = " State", replacement = "")
pop_table <- pop_table %>% select(-state_temp, -pop_2006_temp, - pop_2016_temp) %>%
  select(state, pop_2006, pop_2016) %>%
  mutate(state_fix = state)

pop_table$state_fix[which(pop_table$state_fix == "Nasarawa")] <- "Nassarawa"

head(pop_table)

Combining everything

nigeria_pop <- nigeria %>% 
    left_join(pop_table, by = c("name" = "state_fix"))

plot(nigeria_pop["pop_2016"], key.pos = 1, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)

tm_shape(nigeria_pop) +
    tm_polygons(col = "pop_2016", 
                style = "order",
                title = "Population",
                palette = "Blues") +
   tm_style("cobalt") + 
   tm_text("state", size = 0.7) +
  tm_credits("Source: Wikipedia - List of Nigerian states by population",
             position = c("LEFT", "BOTTOM")) + 
  tm_layout(title= "Nigeria Population by States", 
            title.position = c('right', 'top') ,
            inner.margins = c(0.02, .02, .1, .15))

COVID example

covid19_daily <- refresh_coronavirus_jhu()

head(covid19_daily)
df <- covid19_daily %>%
  filter(location_type == "country") %>%
  group_by(location, data_type) %>%
  summarise(cases = sum(value),
            .groups = "drop") %>%
  pivot_wider(names_from = data_type, values_from = cases) %>%
  setNames(c("country", "total_cases", "total_death"))
  
  

head(df)
df1 <- covid19_daily %>%
  filter(location_type == "country") %>%
  group_by(location, location_code, data_type) %>%
  summarise(cases = sum(value),
            .groups = "drop") %>%
  pivot_wider(names_from = data_type, values_from = cases) %>%
  setNames(c("country", "total_cases", "total_death"))



world_covid <- world %>% 
  select(country = sovereignt, geometry) %>%
  left_join(df, by = "country")


plot(world_covid)

plot(world_covid[, c("total_cases")])

Data Visualization

world <- ne_countries(type = 'countries', scale = 'large')
sp::plot(world)
## Warning in wkt(obj): CRS object has no comment

world <- ne_countries(type = 'countries', scale = 'small', returnclass = "sf")
world$random <- stats::rnorm(n = nrow(world))
sf::plot_sf(world)

sp::plot(world[, "random"])

sp::plot(world[which(world$region_un == "Africa"), "random"])

x1 <- rnaturalearth::ne_countries(country = 'Nigeria', type='countries')
sp::plot(x1)
## Warning in wkt(obj): CRS object has no comment

# x2 <- rnaturalearth::ne_states(country = 'Nigeria')