In the zipfile countries.zip is a global shapefile with various socio-economic indicators for different countries. Load this file into R and make plots of any two of the following variables (the variable names are given in brackets). Try different color scales and transformations of the data to get the most informative maps
Median GDP: (gdp_md_est), Population: (pop_est), Income group: (income_grp)
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(ggplot2)
library(RColorBrewer)
library(viridis)
## Loading required package: viridisLite
library(ggsn)
## Loading required package: grid
## The legacy packages maptools, rgdal, and rgeos, underpinning this package
## will retire shortly. Please refer to R-spatial evolution reports on
## https://r-spatial.org/r/2023/05/15/evolution4.html for details.
## This package is now running under evolution status 0
## Please note that 'maptools' will be retired during October 2023,
## plan transition at your earliest convenience (see
## https://r-spatial.org/r/2023/05/15/evolution4.html and earlier blogs
## for guidance);some functionality will be moved to 'sp'.
## Checking rgeos availability: FALSE
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
countries <- st_read("countries/countries/countries.shp")
## Reading layer `countries' from data source
## `C:\Users\sherl\Documents\Geog 6680 - R\Mod12\countries\countries\countries.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 177 features and 64 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
## CRS: NA
countries = countries[ , c("name", "gdp_md_est", "pop_est", "income_grp")]
ggplot() + geom_sf(data=countries, aes(fill = gdp_md_est)) +
scale_fill_viridis(option = "viridis", name = "Estimated Median GDP") + theme_bw() +
ggtitle("Estimated Median GDP", "by Country") +
north(countries, location = "topright", symbol = 2, scale = 0.1)
It is very difficult to see the differences between the GDPs for the countries because there is such a large range between the poorest and richest countries. Taking the log() of the GDP data will make for a better scale to visualize the differences in the GDPs.
# make columns for the log of the population and gdp variables to better display differences between the countries
# first check for values <= 0
countries[countries$gdp_md_est < 1, ]
## Simple feature collection with 1 feature and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -17.06342 ymin: 20.99975 xmax: -8.665124 ymax: 27.65643
## CRS: NA
## name gdp_md_est pop_est income_grp geometry
## 138 W. Sahara -99 -99 5. Low income MULTIPOLYGON (((-8.794884 2...
countries[countries$pop_est < 1, ]
## Simple feature collection with 1 feature and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -17.06342 ymin: 20.99975 xmax: -8.665124 ymax: 27.65643
## CRS: NA
## name gdp_md_est pop_est income_grp geometry
## 138 W. Sahara -99 -99 5. Low income MULTIPOLYGON (((-8.794884 2...
Looks like W. Sahara is going to cause an issue. I will remove W. Sahara from the dataset, then take the log() of the population and GDP data for the remaining countries.
# remove W. Sahara
countries2 <- countries[countries$pop_est > 0, ]
# take logs of population and gdp columns
countries2$log_gdp_md_est = log(countries2$gdp_md_est)
countries2$log_pop_est = log(countries2$pop_est)
# create GDP and population maps
ggplot() + geom_sf(data=countries2, aes(fill = log_gdp_md_est)) +
scale_fill_viridis(option = "viridis", name = "log(Estimated Median GDP)") + theme_bw() +
ggtitle("World Estimated Median GDP", "by Country") +
north(countries2, location = "topright", symbol = 2, scale = 0.1)
ggplot() + geom_sf(data=countries2, aes(fill = log_pop_est)) +
scale_fill_viridis(option = "inferno", name = "log(Estimated Population)") + theme_bw() +
ggtitle("Estimated Population", "by Country") +
north(countries2, location = "topright", symbol = 2, scale = 0.1)
The scales for these maps look much better. It is easier to see the differences between the countries for the two data sets.
I will also create a map for displaying the countries by their income groups.
ggplot() + geom_sf(data=countries, aes(fill = income_grp)) +
scale_fill_brewer(palette = "Dark2", name = "Income Group") + theme_bw() +
north(countries, location = "topright", symbol = 2, scale = 0.1)
The NetCDF file air.mon.mean.nc contains air temperature data from the NCAR NCEP project, but as mean monthly temperature for every year between 1948 and the present. Using the functions introduced in this lab, read in the data and make a dataset with the globally averaged temperature for every layer in the file (i.e. from January 1948 to present). Use this data to make a line plots, either using base R or ggplot2
library(terra)
## terra 1.7.29
##
## Attaching package: 'terra'
## The following object is masked from 'package:ggsn':
##
## north
## The following object is masked from 'package:grid':
##
## depth
# read in temperature data
air <- rast("air.mon.mean.nc")
# calculate mean monthly temp for each layer
mTemp = global(air, mean)
mTemp$Date = time(air)
ggplot(mTemp, aes(x=Date, y=mean)) + geom_line() +
xlab("Time") + ylab("Temperature (deg C)") +
ggtitle("Global Mean Temperature from 1948 to 2016") +
theme_bw() +
scale_x_datetime(date_breaks = "5 years", date_labels = "%Y")