This demonstrates the basics on how to produce graphs and maps for population data in ‘R Markdown’.

Load packages

First, we load the R packages that will be used to read in, view, transform and map the data.

library(tmap)
library(sf)
library(spData)
library(dplyr)
library(RColorBrewer)

Load population data

The population data for Malawi for 2019 is contained in the ‘census_dist’ shapefile. This layer shows the distribution of the population over the country of Malawi on the basis of district boundary. Data producer is the National Statistical Office.

pop_shapefile <- st_read(dsn = getwd(), layer = "census_dist")
## Reading layer `census_dist' from data source `C:\Users\cnkolokosa\Documents\R' using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 32.67162 ymin: -17.12628 xmax: 35.91842 ymax: -9.363662
## geographic CRS: WGS 84

Take a glimpse of the population data

Here, we can see that the shapefile is a polygon with 6 features and 5 fields containing field ID, object ID, district name, total population and population density for each district, and geometry.

glimpse(pop_shapefile)
## Rows: 32
## Columns: 6
## $ fid        <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1...
## $ OBJECTID   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1...
## $ DISTRICT   <chr> "Chitipa", "Karonga", "Nkhata Bay", "Rumphi", "Mzimba", ...
## $ Total_Popu <dbl> 194707, 297694, 236978, 187137, 795708, 10426, 707862, 3...
## $ PopDensity <dbl> 45.83976, 87.28320, 56.81654, 40.23334, 74.91392, 514.41...
## $ geometry   <MULTIPOLYGON [°]> MULTIPOLYGON (((33.76977 -1..., MULTIPOLYGO...

Map the total population and population density for each district

Using ‘tmap’ package we produce a choropleth map colored in proportion to a statistical variable that represents an aggregate summary of a geographic characteristic within each district, in this case population density and total population.

total_pop <- tm_shape(pop_shapefile)+
            tm_fill(col = "Total_Popu",
                    style = "quantile",
                    palette = "Oranges")+
            tm_borders(col ="burlywood", alpha = .7)+
            tmap_style("cobalt")+
            tm_text(text = "DISTRICT", size = 0.4)+
            tm_scale_bar(position=c("left", "bottom"))

pop_density <- tm_shape(pop_shapefile)+
               tm_fill(col = "PopDensity",
                       style = "quantile",
                       palette = "Oranges")+
               tm_borders(col ="burlywood", alpha = .7)+
               tmap_style("cobalt")+
               tm_text(text = "DISTRICT", size = 0.4)+
               tm_scale_bar(position=c("left", "bottom"))

tmap_arrange(total_pop, pop_density)
Fig1. Choropleth map depicting variability of total population and population density per sq.km in Malawi by district as of 2019

Fig1. Choropleth map depicting variability of total population and population density per sq.km in Malawi by district as of 2019

Remove geomtery from the population shapefile and coerce it to data frame

Here, we drop the geometry column from the population ploygon to enable us plot bar graphs for population density and total population by district using Plotly

total_pop <- st_set_geometry (pop_shapefile, NULL) %>% 
  select(DISTRICT, Total_Popu)


pop_density <- st_set_geometry (pop_shapefile, NULL) %>% 
  select(DISTRICT, PopDensity)

Bar graph showing population density for each district

Here, we observe that population density is significantly high in cities, such as Lilongwe (capital city for Malawi), Blantyre City (central business district), Mzuzu City and Zomba City.

plotly::plot_ly(data = pop_density,
                y = ~DISTRICT,
                x = ~PopDensity,
                type = "bar",
                orientation = 'h',
                name = "") %>% 
  plotly::layout(
    title = "Population Density in Districts of Malawi, 2019",
    xaxis = list(title = "Population density per sq.km"),
    yaxis = list(title = ""))

Fig2. Population density for Malawi by district, 2019

Bar graph showing total population for each district

Here, we observe that total population is significantly high in cities and areas neighbouring urban centers

plotly::plot_ly(data = total_pop,
                y = ~DISTRICT,
                x = ~Total_Popu,
                type = "bar",
                orientation = 'h',
                name = "") %>% 
  plotly::layout(
    title = "Total Population in Districts of Malawi, 2019",
    xaxis = list(title = "Total population"),
    yaxis = list(title = ""))

Fig3. Total Population for Malawi by district, 2019