```{r setup, include=FALSE}
# code chunk specifies whether the R code, warnings, and output
# will be included in the output files.
if (!require("tidyverse")) {
install.packages("tidyverse")
library(tidyverse)
}
if (!require("knitr")) {
install.packages("knitr")
library(knitr)
}
if (!require("cowplot")) {
install.packages("cowplot")
library(cowplot)
}
if (!require("latex2exp")) {
install.packages("latex2exp")
library(latex2exp)
}
if (!require("plotly")) {
install.packages("plotly")
library(plotly)
}
if (!require("gapminder")) {
install.packages("gapminder")
library(gapminder)
}
if (!require("png")) {
install.packages("png") # Install png package
library("png")
}
if (!require("RCurl")) {
install.packages("RCurl") # Install RCurl package
library("RCurl")
}
if (!require("colourpicker")) {
install.packages("colourpicker")
library("colourpicker")
}
if (!require("gifski")) {
install.packages("gifski")
library("gifski")
}
if (!require("magick")) {
install.packages("magick")
library("magick")
}
if (!require("grDevices")) {
install.packages("grDevices")
library("grDevices")
}
### ggplot and extensions
if (!require("ggplot2")) {
install.packages("ggplot2")
library("ggplot2")
}
if (!require("gganimate")) {
install.packages("gganimate")
library("gganimate")
}
if (!require("ggridges")) {
install.packages("ggridges")
library("ggridges")
}
if (!require("sf")) {
install.packages("sf")
library("sf")
}
if (!require("tigris")) {
install.packages("tigris")
library("tigris")
}
if (!require("mapview")) {
install.packages("mapview")
library("mapview")
}
# knitr::opts_knit$set(root.dir = "C:/Users/75CPENG/OneDrive - West Chester University of PA/Documents")
# knitr::opts_knit$set(root.dir = "C:\\STA490\\w05")
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
result = TRUE,
message = FALSE)
```
\
\
\
\
# Introduction
In this note, we illustrate how to add updated information to the existing map file and use it to plot maps with updated information.
# Existing Base Map File and New Information
We use the built-in world map file, `World` , from the package `sf`(simple features), a standardized way to encode spatial vector data. The `World` data set has 17 variables including country names and the corresponding abbreviations, population densities, geometry (information of geo-polygon with longitude and latitude), etc. The data set has 177 countries. We will use the country names to define a primary key merge the new data (with updated information) with the existing data.
The new information to be included in the new data set is based on several data sets available at
The information we will add to the `World` data set is:
1. Income from years 2000, 2005, 2010, 2015 and name them as inc00, inc05, inc10, inc15.
2. Life expectancy from years 2000, 2005, 2010, 2015 and name them as life00, life05, life10, life15.
3. Primary defined from the country names and their abbreviations.
```{r}
#data(World)
World0 <- st_read(system.file("shapes/world.gpkg", package="spData"))
```
```{r}
inc <- read.csv("https://projectdat.s3.amazonaws.com/income_per_person.csv")
income <- data.frame(country = gsub(" ", "", inc$geo), inc00 = inc$X2000, inc05 = inc$X2005, inc10 = inc$X2010, inc15 = inc$X2015)
###
lifexp <- read.csv("https://projectdat.s3.amazonaws.com/life_expectancy_years.csv")
life.exp <- data.frame(country = gsub(" ", "", lifexp$geo), lif00 = lifexp$X2000, lif05 = lifexp$X2005, lif10 = lifexp$X2010, lif15 = lifexp$X2015)
###
pop <- read.csv("https://projectdat.s3.amazonaws.com/population_total.csv")
popsize <- data.frame(country = gsub(" ", "", pop$geo), pop00 =pop$X2000, pop05 = pop$X2005, pop10 = pop$X2010, pop15 = pop$X2015)
###
region <- read.csv("https://projectdat.s3.amazonaws.com/countries_total.csv")
regions <- data.frame(country = gsub(" ", "", region$name), iso_a3 = region$alpha.3)
###
IncLifeExp <- merge(income, life.exp, by = 'country')
IncLifeRegion <- merge(IncLifeExp, regions, by = 'country')
IncLifRegPop <- merge(IncLifeRegion, popsize, by = 'country')
IncLifRegPop$iso_a2 <- substr(IncLifRegPop$iso_a3, 1,2)
###
myWorld <- merge(World0, IncLifRegPop, by = 'iso_a2')
```
The next data set contains the geocode of the world capital cities. This information will be used to create pop-ups to include specific information in the data. The geocode of the capital city can be found at . I also placed a copy of the data set at
```{r}
geocode <-read.csv("https://raw.githubusercontent.com/pengdsci/sta553/main/map/WorldCapitalGeocode.csv")
#geometry = paste('POINT (',CapitalLongitude ,',',CapitalLatitude,')')
#geocode$geometry = geometry
geocode$country <- gsub(" ", "", geocode$CountryName)
capital <- st_as_sf(geocode, coords = c("CapitalLongitude", "CapitalLatitude"), crs = 4326)
###
IncLifeRegionCap <- merge(capital, IncLifeRegion, by = 'country')
IncLifeRegCapPop <- merge(IncLifeRegionCap, popsize, by = 'country')
```
# Thematics Map
```{r fig.align='center', fig.height=6, fig.width=8}
library(tmap)
##
tmap_mode("view") # "view" gives interactive map;
#tmap_style("classic") ## tmap_style set to "classic"
## other available styles are: "white", "gray", "natural",
## "cobalt", "col_blind", "albatross", "beaver", "bw", "watercolor"
tmap_options(bg.color = "skyblue",
legend.text.color = "white")
##
tm_shape(myWorld) +
tm_polygons("lifeExp",
legend.title = "Life Expectancy") +
tm_layout(bg.color = "gray",
inner.margins = c(0, .02, .02, .02)) +
tm_shape(IncLifeRegCapPop) +
tm_symbols(col = "purple",
size = "pop15",
scale = .5,
alpha = 0.5,
popup.vars=c("CapitalName", "pop15", "inc00", "inc05", "inc10","inc15", "lif00","lif05","lif10", "lif15"))
```
# Philadelphia Neighborhood Shapefiles
We can use the shapefile of any place to draw the base map of the place. For example, we can find the shapefile of the Philadelphia neighborhood at and draw the map of the Philadelphia neighborhood using the following map.
```{r}
library(sf)
philly <- st_read("/Users/chengpeng/Downloads/covid_vaccines_by_census_tract/covid_vaccines_by_census_tract.shp")
```
```{r fig.align='center', fig.height=6, fig.width=8}
library(tmap)
tm_shape(philly) +
tmap_options(check.and.fix = TRUE) +
tm_polygons(border.col = "red",
border.alpha = 0.5) +
tm_layout(bg.color = "skyblue",
aes.color = c(fill = "skyblue", borders = "grey40",
dots = "black", lines = "red", text = "black", na = "grey70"),
inner.margins = c(0, .02, .02, .02))
```