R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

##You will need to load the following packages.

library(raster)
library(knitr)
library(rmarkdown)
library(tinytex)
library(sf)
library(sp)
library(spData)
library(tidyverse)
library(dplyr)
knitr::opts_chunk$set(cache=TRUE)  # cache the results for quick compiling

##Background/Objective: Identify the hottest country on each continent (not counting Antarctica) by intersecting a set of polygons with a raster image and calculating the maximum raster value in each polygon.

##I used data from WorldClim.org to create our map and table.

data(world) #load 'world' data from sp Data package
tmax_monthly <-getData(name = "worldclim", var="tmax", res=10)
knitr::opts_chunk$set(cache=TRUE)  # cache the results for quick compiling

##Step 1: Prepare country polygon data (the world object).

worldmap=map_data("world")

world_new <- filter(world,name_long != "Antarctica")
world_spatial <- as(world_new,"Spatial")
knitr::opts_chunk$set(cache=TRUE)  # cache the results for quick compiling

##Step 2:Prepare Climate Data. I downloaded and loaded the WorldClim maximum temperature dataset at the lowest resolution (10 degrees). Uploading the data this way saves time! Then I plotted the annual temperature data to get an idea of what it should look like.

tmax_monthly <-getData(name = "worldclim", var="tmax", res=10)
tmax_monthly_c = tmax_monthly*0.1
tmax_annual <- max(tmax_monthly_c)
names(tmax_annual) <- "tmax"
plot(tmax_annual)+
  title("Max Temperatures Annual in degrees Celsius")

## integer(0)
knitr::opts_chunk$set(cache=TRUE)  # cache the results for quick compiling

##Step 3: ##Calculate the maximum temperature observed in each country.

max_temp<- raster::extract(tmax_annual,world_spatial,fun=max,small = T, na.rm=T,sp=T)
max_temp <- st_as_sf(max_temp)
knitr::opts_chunk$set(cache=TRUE)  # cache the results for quick compiling

##Step 4 : ##Communicating results. I made a table that lists the hottest country on each continent. I also made a world map that shows the maximum temperature in each country.

hottest_countries <- max_temp%>%
  group_by(continent)%>%
  #arrange(desc(tmax))%>%
  top_n(1,tmax) %>% 
  select(continent,name_long,tmax) %>%  #selects the columns necessary for an audience to interpret clearly!
  st_drop_geometry()
colnames(hottest_countries) <- c("Continent", "Country", 'Max Temperature')
knitr::kable(hottest_countries)
Continent Country Max Temperature
North America United States 44.8
South America Argentina 36.5
Seven seas (open ocean) French Southern and Antarctic Lands 11.8
Africa Algeria 48.9
Asia Iran 46.7
Europe Spain 36.1
Oceania Australia 41.8
ggplot(data=max_temp, aes(fill=tmax))+
  geom_sf()+
  scale_fill_viridis_c(name="Annual\nMaximum\nTemperature (C)")+
  theme(legend.position='bottom')

knitr::opts_chunk$set(cache=TRUE)  # For future reference you only have to do this once at the top of the doc!

##Conclusion We have found that the hottest country on each continent are as follows: United States, Argentina, French Southern and Antarctic Lands, Algeria, Iran, Spain and Austrailia.