Philippa Markovics / Apr 16 2019

World Population Density

This notebook shows the world’s population density in 2017 as choropleth map. It is based on a data set provided (as CSV file) by the World Bank which is uploaded here:

World Population Density.csv

Cleaning the Data

In a first step, the R programming language and tidyverse library are used to prepare the CSV file provided by the World Bank for plotting.

library("tidyverse")

The CSV’s first 4 lines are skipped because they contain meta information that is irrelevant to the visualization.

data <- read_csv(
World Population Density.csv
, skip=4) data
0 items

Looking at the data reveals there are plenty of NA values that need to be removed and the data set will be reduced to include only what the plot needs:

data <- data %>%
	select(c(`Country Name`, `Country Code`, `2017`)) %>%
	rename(Density="2017") %>%
	drop_na()
data
0 items

Once the data is in a useful form, it will be saved out to a new CSV file to be used for visualization using Python and plotly later. Saving it to /results will put it into Nextjournal’s content-addressed storage and makes the file available to other programming language runtimes, like Python:

write.csv(data, file="/results/cleaned.csv")
0 items

Population Density Map

Finally, Python and pandas are used to read in our cleaned CSV as dataframe. plotly takes care of rendering an interactive choropleth map:

1.6s
Python
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd

df = pd.read_csv(
cleaned.csv
) data = [go.Choropleth( locations = df['Country Code'], z = df['Density'], zmin = 0.0, zmax = 300.0, colorscale = 'Reds', text = df['Country Name'], marker = go.choropleth.Marker( line = go.choropleth.marker.Line(color = 'rgb(180,180,180)', width = 0.5) ), colorbar = go.choropleth.ColorBar( title = 'People per sqkm' ), )] layout = go.Layout( geo = go.layout.Geo( showframe = False, showcoastlines = False, projection = go.layout.geo.Projection(type = 'mercator') ), margin = go.layout.Margin( l = 0, r = 0, b = 0, t = 0, pad = 0 ) ) go.Figure(data = data, layout = layout)