library(rgdal)
v = read.csv('data.csv', 1, encoding = 'UTF-8')
v
library(gstat)
library(sp)
data(meuse)
head(meuse)
coordinates(meuse) <- c("x","y")
class(meuse)
spplot(meuse,"cadmium")
plot(v,plot.number = T)
v <- variogram(log(meuse$cadmium) ~ 1,meuse, width = 100)
plot(v, plot.number = T)
m1 <- vgm(psill = 1.5,model = "Sph",
range = 1400,nugget = 0.5)
m2 <- fit.variogram(v,m1)
plot(v,model = m2)
m2
data(meuse.grid)
data(meuse)
head(meuse.grid)
kr <- krige(log(meuse$cadmium) ~ 1,
loc = ~ x+y,
data = meuse,
newdata = meuse.grid,
model = m2)
head(kr)
library(lattice)
levelplot(var1.pred ~ x + y,kr )
setwd("F:/Rpeng/21")
library(raster)
library(sp)
library(rgdal)
library(gstat)
library(raster)
library(maptools)
setwd("D:/CBR/2016Krige")
data<-read.table("2016_ST.csv",header=TRUE,sep=",")
head(data)
data$time<-paste(data$year,data$month,data$day,sep='_')
head(data)
bound<-readOGR("CBH.shp")
raster0 <- raster("D:/CBR/2016CCS_CUT/clip_CCS_1d20160621.tif")
summary(raster0)
blank_raster<-raster0
values(blank_raster)<-0
bound_raster<-rasterize(bound,blank_raster)
bound_raster[!(is.na(bound_raster))] <- 0
Grid<-as(bound_raster,"SpatialGridDataFrame")
grd_LL<-rasterToPoints(raster0,spatial=F)
grd_LL<-grd_LL[,-3]
for(i in 1:366)
{
dataone=NULL
index=which(data$day1==i)
dataone=data[index,]
time_i<-unique(dataone$time)
st_judgement<-length(unique(dataone$ST))
if (st_judgement>=5)
{
dsp <- SpatialPoints(dataone[,2:3], proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))
dsp <- SpatialPointsDataFrame(dsp,dataone)
v<- variogram(ST ~ 1, data =dsp)
plot(v)
v.fit<-fit.variogram(v,model=vgm(model = "Sph", psill=1.5, range = 6, kappa = 0.5),fit.sills = 4500)
plot(v,v.fit)
v.fit
kri<-krige(formula=ST~1,model=v.fit,locations=dsp,newdata=Grid,nmax=12, nmin=8)
spplot(kri["var1.pred"])
writeRaster(raster(kri["var1.pred"]),paste
("./","ST_K_",time_i,".tif",sep=""))
write.csv(data.frame(grd_LL,kri$var1.pred),paste
("./","ST_pred_",time_i,".csv",sep=""))
write.csv(data.frame(grd_LL,kri$var1.var),paste
("./","ST_var_",time_i,".csv",sep=""))
} else
{writeRaster(raster(blank_raster),paste
("./","ST_0_",time_i,".tif",sep=""))
write.csv(data.frame(grd_LL,blank_raster@data@values),paste
("./","ST_0pred_",time_i,".csv",sep=""))
write.csv(data.frame(grd_LL,blank_raster@data@values),paste
("./","ST_0var_",time_i,".csv",sep=""))}}
#作者:北师大申泽西
setwd("F:/Rpeng/21")
library(rgdal)
library(tmap)
library(raster)
library(ggplot2)
library(maptools)
library(maps)
library(gstat)
library(RColorBrewer)
ex = extent(105,112,42,47)
shp = as(ex,'SpatialPolygons')
crs(shp) = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
long = runif(20,105,112)
lat = runif(20,42,47)
pr = runif(20,0,15)
df = data.frame(long = long,lat = lat,pr = pr)
head(df)
long_shp = extent(shp)[1:2]
lat_shp = extent(shp)[3:4]
long_p = seq(min(long_shp),max(long_shp),by =resolution )
lat_p = seq(min(lat_shp),max(lat_shp),by = resolution)
s_p = expand.grid(long_p,lat_p)
inusa = map.where(x = s_p[,1],y = s_p[,2])
inusa = !is.na(inusa)
s_p = s_p[inusa,]
colnames(s_p) = c('long','lat')
coordinates(s_p) = ~ long + lat
gridded(s_p) = T
fullgrid(s_p) = F
proj4string(s_p) = crs(shp)
pr = as.numeric(df[,3])
na_index = which(is.na(pr))
df = data.frame(long=df[,1],lat=df[,2],pr=as.numeric(df[,3]))
df2 = df
if(length(na_index)>0){
df = df[-na_index,]
}
coordinates(df) = ~ long+lat
proj4string(df) = crs(shp)
print("Pass Phase 2")
f1 = as.formula(pr ~ long+lat)
var.sampl = variogram(f1,df,cloud =F,
cutoff = 100000,
width = 89900)
dat.fit = fit.variogram(var.sampl,fit.ranges = F,
fit.sills = F,
vgm(psill = 14,model = 'Sph',
range = 590000,
nugget = 0))
dat.krig = krige(f1,df,s_p,dat.fit)
dat.r = raster(dat.krig)
writeRaster(dat.r,'raster.tif',format = 'GTiff',overwrite = T)
krg.res = as.data.frame(dat.r,xy = T)
colnames(krg.res) = c('long','lat','pr')
mycolor = colorRampPalette(brewer.pal(11,'Spectral'))(12)
p = ggplot()+
geom_tile(data = krg.res,aes(x = long,y = lat,fill = pr),
color = 'transparent')+
geom_point(data = df2,aes(x = long,y = lat),color = 'black',
shape =1, size = 5)+
scale_fill_gradientn(colours = mycolor)+
theme_bw()+
theme(
axis.text = element_text(face = 'bold',color = 'black',size = 12),
axis.title = element_text(face = 'bold',color = 'black',size = 14, hjust = .5),
legend.text = element_text(face = 'bold',color = 'black',size = 12),
legend.title = element_blank(),
legend.position = 'bottom',
legend.direction = 'horizontal',
legend.key.width = unit(2,'cm')
)+
xlab('Longitude')+
ylab('Latitude')
p
png('krige.png',
height = 20,
width = 20,
units = 'cm',
res = 800)
print(p)
dev.off()
library(ggplot2)
data("USArrests")
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimes$tempvar <- "Kingjames Kingpeng"
crimesm <- reshape2::melt(crimes, id = 1)
states_map <- map_data("state")
USA_map <-ggplot(crimes, aes(map_id = state)) +
geom_map(aes(fill=Murder), map=states_map,colour="cyan",size = 0.5) +
scale_fill_distiller(palette = "RdPu",direction= 1) +
expand_limits(x = states_map$long, y = states_map$lat)
USA_map
USA_map+ facet_grid(. ~ tempvar) +
theme(strip.background = element_rect(fill="#999999"),
strip.text = element_text(size=15, colour="white"))
library(ggplot2)
library(sf)
library(magrittr)
library(rgdal)
library(dplyr)
path1<-"F:/Rpeng/21/prov_centroids.csv"
Prov_centers<-read.csv(path1,stringsAsFactors=FALSE)
str(Prov_centers)
path2<-"F:/Rpeng/21/china6.json"
China_1<-st_read(path2,stringsAsFactors=FALSE)
mydata_1<-data.frame(
id=1:34,
name=Prov_centers$name,
scale=runif(34,100,200)%>%round(),
Scope=rep(LETTERS[1:5],length=34))
mydata_2<-left_join(Prov_centers,mydata_1,by="name")
mydata_3<-left_join(China_1,mydata_2,by="name")
mydata_3$tempvar <- "Kingjames Kingpeng"
ggplot(mydata_3)+geom_sf(aes(fill=scale),color="cyan")+
coord_sf()+scale_fill_continuous(type="viridis")
ggplot(mydata_3)+geom_sf(aes(fill=scale),color="cyan")+
coord_sf()+scale_fill_continuous(type="viridis")+ facet_grid(. ~ tempvar) +
theme(strip.background = element_rect(fill="#999999"),
strip.text = element_text(size=15, colour="white"))
setwd("F:/Rpeng/21")
library(ggplot2)
df <- data.frame(treatment = factor(c(1, 1, 1, 2, 2, 2, 3, 3, 3)),
response = c(2, 5, 4, 6, 9, 7, 3, 5, 8),
group = factor(c(1, 2, 3, 1, 2, 3, 1, 2, 3)),
se = c(0.4, 0.2, 0.4, 0.5, 0.3, 0.2, 0.4, 0.6, 0.7))
head(df)
df$tempvar <- "Kingjames Kingpeng"
ggplot(data = df, aes(x = treatment, y = response, fill = group)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar(aes(ymax = response + se, ymin = response - se),
position = position_dodge(0.9), width = 0.15) +
scale_fill_brewer(palette = "Set1")+ facet_grid(. ~ tempvar) +
theme(strip.background = element_rect(fill="#999999"),
strip.text = element_text(size=15, colour="white"))