How to draw spatial maps in R using ggmap without Google API key? | IMD | StatswithR | Arnab Hazra
ฝัง
- เผยแพร่เมื่อ 15 ต.ค. 2024
- Here we explain how to get freely gridded rainfall data from India Meteorological Department and how to generate presentation or publication-quality spatial rainfall map across the study domain (India, here) in R/R-studio using ggmap without using Google API key. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.
#datascience #datavisualization #visualization #ggplot2 #tidyverse #terrain #mapping #spatial #statistics #spatial #covariates #India #precipitation #rainfall #rcoding #GoogleAPIkey #IMD #IMDPune #griddedrainfall
Data link: www.imdpune.go...
library(ncdf4)
fname = "_Clim_Pred_LRF_New_RF25_IMD0p252020.nc"
ncin = nc_open(fname)
prec = ncvar_get(ncin)
nc_close(ncin)
dim(prec)
prec.total = apply(prec, 2, rowSums)
dim(prec.total)
S = as.matrix(expand.grid(ncin$dim$LONGITUDE$vals, ncin$dim$LATITUDE$vals))
Y = c(prec.total) / 10
library(ggmap)
keep = which(!is.na(Y))
S = S[keep, ]
Y = Y[keep]
data = data.frame(lon = S[ , 1], lat = S[ , 2], value = Y)
colnames(data) = c("Longitude", "Latitude", "Rainfall")
head(data)
indbox = make_bbox(lon = c(min(S[ , 1]), max(S[ , 1])), lat = c(max(S[ , 2]), min(S[ , 2])), f = .1)
ind = get_map(location = indbox, zoom = 5, maptype = "terrain")
p = ggmap(ind)
ggsave(p, filename = "ggplot_ind1.pdf", height = 8.5, width = 8)
data = data.frame(lon = S[ , 1], lat = S[ , 2], value = Y)
p = ggmap(ind) + geom_tile(data, mapping = aes(x = lon, y = lat, fill = value))
ggsave(p, filename = "ggplot_ind2.pdf", height = 8, width = 8)
p = ggmap(ind) + geom_tile(data, mapping = aes(x = lon, y = lat, fill = value), width = 0.27, height = 0.27)
ggsave(p, filename = "ggplot_ind3.pdf", height = 8, width = 8)
p0 = ggmap(ind) + geom_tile(data, mapping = aes(x = lon, y = lat, fill = value), width = 0.27, height = 0.27) + xlab("Longitude") + ylab("Latitude") + ggtitle("Annual precipitation (2020)") +
theme(plot.title = element_text(hjust = 0.5, size=25),
axis.text=element_text(size=20), axis.title=element_text(size=20))
ggsave(p0, filename = "ggplot_ind4.pdf", height = 8, width = 8)
library(grDevices)
p = p0 + scale_fill_gradientn(colours = topo.colors(30))
ggsave(p, filename = "ggplot_ind5.pdf", height = 8, width = 8)
p1 = p0 + scale_fill_gradientn(colours = topo.colors(30), name = "Rainfall (cm)") + theme(legend.key.size = unit(0.3, "in"), legend.text = element_text(size=15), legend.title = element_text(size=15), legend.position = c(0.8, 0.2))
ggsave(p1, filename = "ggplot_ind6.pdf", height = 8, width = 8)
baseData = map_data("world")
p = p1 + geom_path(data=baseData, aes(x=long, y=lat, group=group), colour="black")
ggsave(p, filename = "ggplot_ind7.pdf", height = 8, width = 8)
Dear Arnab, It is a wonderful tutorial for plotting spatial maps in R by downloading gridded data from IMD website on yearly rainfall in India. As a student learning R, I was trying to follow the instructions. It worked well till a point up to making dataframe of Latitude, Longitude and Rainfall. But when it comes to entering the syntax : ind
Detailed explanation - well done bro!
Well-documented video on IMD Extraction data. Keep making video on Rstudion to extract Chirps, TRMM, IMD data. Do me favours attached the R scripts to extracted gr files, nc file, tiff file. I will help a lots.
Thanks
Thank you for your comment 😊
Thanxxx
Thank you for your comment 😊
ncin = nc_open(fname)
Error in nc_open(fname) : could not find function "nc_open"
> prec = ncvar_get(ncin)
Error in ncvar_get(ncin) : could not find function "ncvar_get"
> nc_close(ncin)
Error in nc_close(ncin) : could not find function "nc_close" facing issues here sir