I have a three column dataset with LAT, LON and Temperature data I would like to produce a raster image that predicts the temperature of the landscape based on 24 data logger data points. The dataset can be accessed here: DATA

This is what I have attempted this far:

Lets try to interpolate the data onto a raster

library (raster)
library (gstat)
library (sp)
#Temp and XY data
temp<-read.csv ('test_temp.csv')
#create a blank raster to the extent of the system
r<- raster (nrows=300, ncols=100, xmn=-84.95, xmx=-84.936, ymn=45.7, ymx=45.74)

#build a prediction model temp ~ LAT*LON
loc<-temp [,c(3,5,6)]
loc<-na.omit (loc)
#TPS model 
tps<-Tps(loc, loc$TemperatureC)
#gstat model
mod1<-gstat (data=temp, formula=TemperatureC ~ 1, locations=loc )
summary (mod1)

r2<-interpolate(r, model=tps)

r2<-interpolate(r, model=mod1)

Ultimatley I want to create a series of rasters of interpolated data to show temperature variation at different times of day. Any ideas on how to do this?

  • You should probably edit this a bit to make it clear you have lat, long, time of day and temperature data in the CSV! Its not three columns! Pasting the first ten or so lines from R into here would help. – Spacedman Jul 29 '17 at 14:48
  • Do you need library(fields) for the Tps function? – Spacedman Jul 29 '17 at 14:53
  • Both your interpolate calls give me errors: Error in scale.default(x, xc, xs) : length of 'center' must equal the number of columns of 'x' for Tps and Error in bbox(dataLst[[1]]$data) : object not a >= 2-column array for mod1. – Spacedman Jul 29 '17 at 14:56
  • @Spacedman: I also get the same errors. This is what I am trying to resolve. – I Del Toro Jul 29 '17 at 14:59

Using an amalgamation of code and examples, I came up with something that looks correct to me:

library(ggplot2) # start needed libraries

my_location<-c(lon= -84.94, lat = 45.72)
basemap <- get_map(location = my_location, zoom = 14, maptype = "hybrid")
plot (basemap)
#load data 
field_data<-read.csv ("test_temp.csv")
test_temp <- field_data # duplicate air temp. data file
test_temp$x <- test_temp$LON # define x & y as longitude and latitude
test_temp$y <- test_temp$LAT
coordinates(test_temp) = ~x + y #set spatial coordinates to create a Spatial object

x.range <- as.numeric(c(-84.94, -84.935)) # map extent
y.range <- as.numeric(c(45.705, 45.735))
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.001), 
                   y = seq(from = y.range[1], to = y.range[2], by = 0.001)) # expand points to grid
coordinates(grd) <- ~x + y 
gridded(grd) <- TRUE
plot(grd, cex = 1.5)
points(test_temp, pch = 1, col = "red", cex = 1)
idw <- idw(formula = TemperatureC ~ 1, locations = test_temp, newdata = grd) # apply idw model for the data
idw.output = as.data.frame(idw)
names(idw.output)[1:3] <- c("long", "lat", "var1.pred") # give names to the modelled variables
# plot results
ggplot() +
  geom_tile(data = idw.output, alpha=0.5, aes(x = long, y = lat, fill=var1.pred, 0)) +
  geom_point(data=field_data, aes(x=LON, y=LAT), shape=21, colour="red") + 
  scale_fill_gradient(low="cyan", high="orange") + theme_bw() 

ggmap(basemap, extent = "device") + 
  geom_tile(data = idw.output, alpha=0.5, aes(x = long, y = lat, fill=var1.pred, 0)) +
  geom_point(data=field_data, aes(x=LON, y=LAT), shape=16, size=.5, colour="red") + 
  scale_fill_gradient(low="cyan", high="red") + theme_bw() 

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.