-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tanzania.R
42 lines (34 loc) · 1.17 KB
/
Tanzania.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# install.packages(c("raster","ggplot2", "magrittr", "sf"))
# remotes::install_github("wmgeolab/rgeoboundaries")
library(raster)
library(ggplot2)
library(magrittr)
library(sf)
library(rgeoboundaries)
# Downloading monthly maximum temperature data
tmax_data <- getData(name = "worldclim", var = "tmax", res = 10)
# Converting temperature values to Celcius
gain(tmax_data) <- 0.1
# Calculating mean of the monthly maximum temperatures
tmax_mean <- mean(tmax_data)
# Downloading the boundary of Nigeria
nigeria_sf <- geoboundaries("Tanzania")
# Extracting temperature data of Nigeria
tmax_mean_ngeria <- raster::mask(tmax_mean, as_Spatial(nigeria_sf))
# Converting the raster object into a dataframe
tmax_mean_nigeria_df <- as.data.frame(tmax_mean_ngeria, xy = TRUE, na.rm = TRUE)
tmax_mean_nigeria_df %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = layer)) +
geom_sf(data = nigeria_sf, inherit.aes = FALSE, fill = NA) +
labs(
title = "Mean monthly maximum temperatures in Tanzania",
subtitle = "For the years 1970-2000"
) +
xlab("Longitude") +
ylab("Latitude") +
scale_fill_gradient(
name = "Temperature (°C)",
low = "#FEED99",
high = "#AF3301"
)