Thank you very much
Please, find the details--
Shapefile--https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip
dput () output is here--
structure(list(Variable = c("Temp", "Temp", "Temp", "Temp", "Temp",
"Temp", "Temp", "Temp", "Temp", "Temp", "Temp", "Temp", "Precip",
"Precip", "Precip", "Precip", "Precip", "Temp", "Temp", "Temp",
"Temp", "Temp", "Temp", "Precip", "Precip", "Precip", "Precip",
"Precip", "Precip", "Precip", "Precip", "Precip", "Precip", "Precip",
"Precip", "Precip", "Precip", "Precip", "Temp", "Temp", "Temp",
"Temp", "Temp", "Temp", "Temp", "Temp", "Temp", "Temp", "Temp",
"Temp"), Century = c(800L, 900L, 1000L, 1100L, 1200L, 1300L,
800L, 900L, 1000L, 1100L, 1200L, 1300L, 900L, 1000L, 1100L, 1200L,
1300L, 800L, 900L, 1000L, 1100L, 1200L, 1300L, 900L, 1000L, 1100L,
1200L, 1300L, 900L, 1000L, 1100L, 1200L, 1300L, 900L, 1000L,
1100L, 1200L, 1300L, 800L, 900L, 1000L, 1100L, 1200L, 1300L,
800L, 900L, 1000L, 1100L, 1200L, 1300L), Longitude = c(12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75,
1, 1, 1, 1, 1, 28.325, 28.325, 28.325, 28.325, 28.325, 28.325,
15.733333, 15.733333, 15.733333, 15.733333, 15.733333, 15.733333,
15.733333, 15.733333, 15.733333, 15.733333, -4.98, -4.98, -4.98,
-4.98, -4.98, 6.8833, 6.8833, 6.8833, 6.8833, 6.8833, 6.8833,
6.8833, 6.8833, 6.8833, 6.8833, 6.8833, 6.8833), Latitude = c(47.5,
47.5, 47.5, 47.5, 47.5, 47.5, 42.5, 42.5, 42.5, 42.5, 42.5, 42.5,
52.5, 52.5, 52.5, 52.5, 52.5, 62.325, 62.325, 62.325, 62.325,
62.325, 62.325, 68.8, 68.8, 68.8, 68.8, 68.8, 68.8, 68.8, 68.8,
68.8, 68.8, 58.15, 58.15, 58.15, 58.15, 58.15, 50.1167, 50.1167,
50.1167, 50.1167, 50.1167, 50.1167, 50.1167, 50.1167, 50.1167,
50.1167, 50.1167, 50.1167), Mean = c(-0.674401224, 0.383627143,
1.472242402, 0.220960495, -0.597933997, -1.520406824, 1.282612958,
1.008766116, -0.762620237, -1.082777938, 0.268867128, -0.714848027,
-1.070371799, -1.006936142, 0.03260452, 0.306612444, 0.083288067,
0.882435346, -0.040712438, 1.274604058, -0.641877141, -1.040919279,
0.784619088, 0.874041638, -0.241763974, 1.065917808, 0.48992382,
-0.642693799, 0.874041638, -0.241763974, 1.065917808, 0.48992382,
-0.642693799, -0.301215399, -1.726692947, 0.515607908, 1.228251917,
0.379726471, -1.103446109, 1.514326234, 0.866236965, -0.722217127,
-0.099543123, -0.45535684, 0.469462913, -0.904574882, -0.870223937,
1.706096929, 0.125953465, -0.526714488)), row.names = c(NA, 50L
), class = "data.frame")
Summary
Script is here--
rm(list=ls(all=TRUE))
dev.off()
#Dependencies
library(sf)
library(sp)
library(rgdal)
library(tidyr)
library(gstat)
library(ggplot2)
library (gplots)
library(ggthemes)
#set working directory
setwd("D:/Spatial_values")
##### Data file ####
file <-read.csv("Europe.csv", header = T, sep = ",")
#### Filter selected colomus
eu_Df <- file %>%
dplyr::select(Variable, Century, Longitude, Latitude, Mean) %>%
filter(!is.na(Mean))
##### IDW----interpolation #########
#countries
countries <- st_read("D:/shape_files/ne_110m_admin_0_countries", layer="ne_110m_admin_0_countries")
# Filter/subset continent/country of choice
sub_Europe <- countries[countries$CONTINENT == "Europe",]
eu_prj <- st_transform(sub_Europe, CRS("+proj=longlat +datum=WGS84 +no_defs"))
# generalization
#eu_cont_smpl <- sf::st_simplify(eu_prj, dTolerance = 200)
eu_cont_smpl <- st_cast(eu_prj, preserveTopology = T, dTolerance = 4000)
# create simple feature with filtered DATA....
eu_sf <- st_as_sf(eu_Df, coords= c("Longitude", "Latitude"),
crs = CRS("+proj=longlat +datum=WGS84 +no_defs")) ##4326
# project lat-lon (WGS84)
eu_sf <- st_transform(eu_sf, CRS("+proj=longlat +datum=WGS84 +no_defs"))
# Get the extent of interpolation area
grd_extent <- st_bbox(eu_cont_smpl)
# create grid
x.range <- as.numeric(c(grd_extent[1], grd_extent[3])) # min/max longitude of the interpolation area
y.range <- as.numeric(c(grd_extent[2], grd_extent[4])) # min/max latitude of the interpolation area
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 500), y = seq(from = y.range[1], to = y.range[2], by = 500)) # expand points to grid
#Convert grid to spatial object
coordinates(grd) <- ~x + y
#gridded(grd) <- TRUE
## convert simple feature to spatial format
glimpse(eu_sf)
eu_sp <- as_Spatial(eu_sf)
est_cont_sp <- as_Spatial(eu_prj)
# grid:
idw_grid <- spsample(est_cont_sp, type = "regular", n = 4000)
# idw
rslt_pt <- gstat::idw(Mean~1, locations=eu_sp, newdata = idw_grid, idp = 2, nmax = length(eu_sp))
# Clean up
rslt_df2 <- as.data.frame(rslt_pt)
names(rslt_df2)[1:4] <- c("Longitude", "Latitude", "pred.mean", "Century")
glimpse(rslt_df2)
### Remove NAs from the cleaned data
rslt_grd_df <- rslt_df2 %>% filter(!is.na(pred.mean))
glimpse(rslt_grd_df)
palett <- rich.colors(40, palette = "temperature", rgb = T)
ggplot() +
theme_map()+
theme(panel.grid = element_line(color = "grey70", size = 0.2), legend.position = "right")+
geom_tile(data=rslt_grd_df, aes(x = Longitude, y = Latitude, fill = pred.mean))+
geom_sf(data = eu_cont_smpl, fill = NA, size = 0.2, colour = "gray45")+
scale_fill_gradientn(colours = palett)+
#facet_wrap(~Century + Variable)+ # I want to get output that fit with this :(
xlim(-30, 70)+
ylim(25, 86)+
theme_classic()