2

I have a csv file contains hourly PM10 concentration of 1 march to 7 march. Please, download from here. I have plotted all the variogram (total 161) in a loop by automap package.

library(sp)
library(gstat)
library(rgdal)
library(automap)
library(latticeExtra)

seoul1to7<-read.csv("seoul1to7.csv")
seoul1to7[seoul1to7==0] <-NA
seoul1to7 <- na.omit(seoul1to7)
seoul1to7_split<-split(seoul1to7,seoul1to7$time) 
seq(seoul1to7_split)



vars<-lapply(seq(seoul1to7_split), function(i)
{
  dat<-seoul1to7_split[[i]]  
  coordinates(dat)<-~LON+LAT
  proj4string(dat) <- "+proj=longlat +datum=WGS84" 
  dat <- spTransform(dat, CRS("+proj=utm +north +zone=52 +datum=WGS84"))
  variogram<-autofitVariogram(log(PM10)~1,dat, model="Sph")
  plot<- plot(variogram,plotit=FALSE, asp=1)

  return(plot)
})
vars[[1]]
vars[[2]]

Here, I can get the individual plot by vars[[1]],vars[[2]]...etc but all the variogram has a same title. Now, I want to plot all the variogram image with different title in a loop. I want my variograms title be like, "Variogram for 2012-03-01 1.00", "Variogram for 2012-03-01 2.00" .....etc.

a<-as.POSIXct(names(seoul1to7_split), format="%Y%m%d%H")
a
hours<-substr(a,1,16)
hours

I keep my desired different title in hours variable like "2012-03-01 01:00", "2012-03-01 02:00", "2012-03-01 03:00" ...etc.

How can I plot all the variograms (total 161) with different title using loop?

Orpheus
  • 329
  • 6
  • 21

1 Answers1

0

The trick is to use png and dev.off:

library(automap)
data(meuse)
coordinates(meuse) = ~x+y

fit_for_model_type = function(model_type) {
    return(autofitVariogram(log(zinc) ~ dist, meuse, model = model_type))
}

model_options = c("Sph", "Exp", "Gau", "Ste")
list_of_variogram_models = lapply(model_options, fit_for_model_type)
names(list_of_variogram_models) = model_options
lapply(names(list_of_variogram_models), function(x) {
    png(filename = sprintf('%s.png', x))
    print(plot(list_of_variogram_models[[x]]))
    dev.off()
})
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149