Overview:

There are two tasks in this homework. The tasks are very open. You must do at least the bare minimum on both tasks (so as to get the practice), and chose ONE of the tasks for somewhat deeper analysis. You choose which one you want to prioritize - whatever inspires you most.

Task 1: Use world bank data to analyze the percentage of urban population by country.

library(haven)
library(ggplot2)
library(dplyr)
library(sf)
library(reshape2)
library(wbstats)
library(gtrendsR)
library(viridis)
library(maps)


index_search <- wbsearch(pattern = "urban population")

world_map <- rnaturalearth::ne_countries(scale = 50, returnclass = "sf")

pol_urban <- wb(country = "countries_only", 
                         indicator = "SP.URB.TOTL.IN.ZS", startdate = 2021, enddate = 2021)

map_urban <- left_join(world_map, pol_urban, by = c("iso_a2" = "iso2c"))

urban_graph <- ggplot(map_urban) +
  geom_sf(aes(fill = value)) +
  scale_fill_viridis("Percentage")+  
  ggtitle("Percentage of Urban Population by Country in 2021") +
  theme_bw()+
  labs(caption = "Data Source: World Bank") + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        plot.caption = element_text(size = 10, face = "bold"))
urban_graph


In the first task I mapped the percentage of urban population by country in year 2021, more than half of the population resides in urban areas in American continents, western Europe and Asia-Pacific countries while Sub-Sahara Africa, Central Asia and South Asian have much lower level in urban population.

country_select <- wb_data(country = c("US", "GB", "JP", "CN", "IN", "BR"),
                 indicator = "SP.URB.TOTL.IN.ZS", start_date = 2001, end_date = 2021)

urban_plot <-ggplot(country_select, aes(x=as.numeric(date), y=SP.URB.TOTL.IN.ZS, color= country)) + 
  geom_line() + ggtitle("Percentage of urban population in select countries")+
  ylab("Urban Population (%)") + xlab("Year") +
  theme_classic() + labs(caption = "Data Source: The World Bank")

urban_plot1 <- urban_plot + scale_x_continuous(limits=c(2001, 2021), breaks=seq(2001, 2021, 4)) 

urban_plot2 <- urban_plot1 + scale_y_continuous(limits=c(0, 100), breaks=seq(0, 100, 10)) 

urban_final <- urban_plot2 + theme(legend.direction = "vertical",
                                  legend.text = element_text(size = 7.5, face = "bold"),
                                  legend.key = element_blank(),
                                  panel.grid.major = element_blank(),
                                  panel.background = element_blank(),
                                  axis.line = element_line(colour = "black"),
                                  plot.title = element_text(size = 13, hjust = 0.5, face = "bold"),
                                  axis.text.x=element_text(size = 9), 
                                  axis.text.y=element_text(size = 9),
                                  axis.title = element_text(size = 10, face = "bold"),
                                  plot.caption = element_text(size = 10, face = "bold"))
urban_final


I investigated the change in percentage of urban population for the six select countries for the past twenty years. China observed the most salient increase in urban population from below 30% to over 60%; India exhibited the lowest percentage of urban population among six countries. All developed countries (United States, United Kingdom, and Japan) have a higher level of urban population. Overall there exists a gap in the percentage of urban population between the three select developed countries and the developing countries, except Brazil.