title | author | date | output |
---|---|---|---|
Precipitation in Kenya |
Sri Ramesh |
4/7/2020 |
html_document |
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(raster)
library(leaflet)
In the following, I pulled WordClim data on the long-term average precipitation in Kenya each January from 1970 to 2000 to illustrate on a leaflet map. The time-series data can be found here: worldclim.org.
KEN_Adm0 <- raster::getData(name = "GADM", country = "KEN", level = 0)
KEN_prec_0.5_bottom <- raster::getData(name = "worldclim", var = "prec", res = 0.5, lat = -3, lon = 39)
KEN_prec_0.5_top <- raster::getData(name = "worldclim", var = "prec", res = 0.5, lat = 3, lon = 35)
KEN_prec_0.5 <- raster::merge(x = KEN_prec_0.5_bottom, y = KEN_prec_0.5_top)
KEN_prec_0.5_Jan <- KEN_prec_0.5[[1]]
KEN_prec_0.5_Jan_inches <- KEN_prec_0.5_Jan * 0.0393701
KEN_prec_1_Jan_inches <- KEN_prec_0.5_Jan_inches %>%
raster::aggregate(fact = 2, fun = sum)
KEN_prec_1_Jan_Crop_Unmasked <- KEN_prec_1_Jan_inches %>% raster::crop(y = KEN_Adm0)
KEN_prec_1_Jan_Crop <- KEN_prec_1_Jan_Crop_Unmasked %>% raster::mask(mask = KEN_Adm0)
basemap <- leaflet() %>% addProviderTiles("CartoDB.Positron")
raster_colorPal_prec_JAN <- colorNumeric(palette = topo.colors(64), domain = values(KEN_prec_1_Jan_Crop),
na.color = NA)
map_kenya <- basemap %>%
addRasterImage(x = KEN_prec_1_Jan_Crop,
color = raster_colorPal_prec_JAN) %>%
addPolygons(data = KEN_Adm0,
fillOpacity = 0,
color = "purple",
weight = 3)
map_kenya <- map_kenya %>%
addLegend(title = "Jan precipitation (in)<br>(1' res)",
values = values(KEN_prec_1_Jan_Crop),
pal = raster_colorPal_prec_JAN)
map_kenya