You are on page 1of 2

rm(list=ls())

#install.packages("fields")

#######Simplest spatial plot#######


library(fields)
#Spatial axes

lon<- c(-116.7, -120.4, -116.7, -113.5, -115.5, -120.8, -119.5, -113.7, -113.7, -
110.7)
lat<- c(45.3, 42.6, 38.6, 42.1, 35.7, 38.9, 36.2, 39, 41.6, 36.9)

#Variable of interest, precipitation


precip<- c(721, 19, 52, 188, 749, 8, 725, 843, 289, 249)

# locations

loc<- cbind(lon, lat)

X11() #new graphic window


plot(loc, main="spatial locations", ylab="latitude", xlab="longitude")

quilt.plot(lon, lat, precip, main="Precipitation", ylab="latitude",


xlab="longitude")

#######Improved than the simplest spatial plot#######


rm(list=ls())

#define spatial locations


x1<- seq(-23,23, length=47)
y1<- seq(-23,23, length=47)

nloc<- length(x1)*length(y1)
locs<- expand.grid(x1,y1)

X11()
plot(locs)

##.....data.....
##setup a covariance function

grid1<- list(x=x1, y=y1)


cov_fn<- Exp.image.cov(grid=grid1, theta=7, setup=TRUE) #exponential
covariance function with
cov_fn1<- Exp.image.cov(grid=grid1, theta=2, setup=TRUE) #exponential
covariance function with

#some range parameter theta


set.seed(235)
Y<- sim.rf(cov_fn)
Y1<- sim.rf(cov_fn1)

X11()
par(mfrow=c(1,2))
image.plot(x1, y1, Y1, main="theta=2")
image.plot(x1, y1, Y, main="theta=7")

##improved legend bar


brks<- seq(-3, 4, by=0.25)

cool = rainbow(sum(brks<0) , start=rgb2hsv(col2rgb('cyan'))[1],


end=rgb2hsv(col2rgb('blue'))[1])
warm = rainbow(sum(brks>0), start=rgb2hsv(col2rgb('red'))[1],
end=rgb2hsv(col2rgb('yellow'))[1])

col_pal<- c(rev(cool), rev(warm))

X11()
image.plot(x1, y1, Y, breaks= brks, col= col_pal, nlevel= length(brks)-1)

You might also like