G. Jan
24 July 2018
This presentation shows the Population World Map per year from 1980 til 2010. The data are provided by data.gov.
The presentation contains the following slides:
The user only needs to input the desired year in order to show the corresponding population world map. Note that missing data appears in grey on the map.
The input year variable is passed on to the server which select the appropriate data from the input data. The input data was downloaded data.gov and contains the population for each country from 1980 until 2010. Country codes are derived from the county name using the package countrycode. Then the population for required year is joined to a map of polygon boundaries using the rworldmap package.
# Input data from data.gov
popt <- read.csv("~/Downloads/populationbycountry19802010millions.csv")
popt <- popt[popt$X!=c("North America","World","Central & South America","World"),]
year <- gsub("X","",names(popt))
# UI
ui <- shinyUI(fluidPage(
# App title
titlePanel("Population World Map per Country"),
sidebarLayout(
# Sidebar panel for inputs - Select year
sidebarPanel("Please select the year you wish to visualize:",br(),br(),
selectInput('year', 'Year', choices = year,selected = 1980), br(),br(),
"The input data is coming from data.gov - Here is the",
a("link", href="https://openei.org/doe-opendata/dataset/a7fea769-691d-4536-8ed3-471e993a2445/resource/86c50aa8-e40f-4859-b52e-29bb10166456/download/populationbycountry19802010millions.csv")
),
#Main panel
mainPanel(
tabsetPanel(id="tp",tabPanel("Map View", plotOutput("map", height="760px", width="1150px")))
)
)
))server <- function(input, output) {
# Read data in csv format
popt <- read.csv("~/Downloads/populationbycountry19802010millions.csv")
# Prep name variables
names(popt) <- gsub("X","",names(popt))
names(popt)[1] <- c("Country")
# From factor to numeric
pop <- as.data.frame(apply(popt[,2:dim(popt)[2]], 2, function(x) as.numeric(as.character(x))))
# Get ISO3 code name for Countries
pop$Country <- gsub("Former U.S.S.R","",popt[,1])
pop$Code <- countrycode(pop$Country, 'country.name', 'iso3c')
# Color palette
mypalette <- brewer.pal(6, "YlOrBr")
# Mapping
observe({
year <- input$year
#join data to a map
popmap <- joinCountryData2Map( pop[,c("Country","Code",year)],
nameJoinColumn="Code",
joinCode="ISO3" )
output$map <- renderPlot({
#plot the map
mapParams <- mapCountryData( popmap,
nameColumnToPlot=year,
mapTitle = "Population per country",
colourPalette = mypalette,
catMethod=c(0,10,20,50,100,500,2000),
oceanCol = "light blue",
missingCountryCol = grey(.85),
addLegend = FALSE)
do.call(addMapLegendBoxes,c(mapParams, x= "left", horiz = FALSE, title = "Population in Millions", cex = 1.5, pt.cex = 2))
})
})
}