[R-sig-Geo] How to plot multiple semi-variogram from a single dataset efficiently in R?

Tim Appelhans tim.appelhans at gmail.com
Thu Aug 27 12:49:11 CEST 2015


Uzzal,
I see that in your lapply() call you create an object called 'plot'. 
This is bad practice as 'plot' is reserved for a function call in base R 
and may get you in trouble in certain circumstances. That is why in the 
code I gave you (and in the code that follows) this object is called 
'plt' rather than plot.

The following will create a variogram for every hour of each day, label 
them according to YYYY-mm-dd HH:MM:SS (standard POSIX datetime format) 
and save each as a file following a naming convetion of 
'plot_YYYYmmddHH.png' to avoid spaces in the file names.

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

seoul311 <- read.csv("Downloads/seoul1to7.csv")
seoul311 <- na.omit(seoul311)

### first we split seoul311 by time into a list rather than subsetting 
manually
seoul311_splt <- split(seoul311, seoul311$time)

a<-as.POSIXct(names(seoul311_splt), format="%Y%m%d%H")
a

vars <- lapply(seq(seoul311_splt), function(i) {

   dat <- seoul311_splt[[i]]
   coordinates(dat) <- ~LON+LAT
   proj4string(dat) <- "+proj=longlat +datum=WGS84"
   dat <- spTransform(dat, CRS("+proj=utm +north +zone=52 +datum=WGS84"))

   variogram <- autofitVariogram(PM10 ~ 1, dat)

   plt <- plot(variogram, plotit = FALSE, asp = 1)
   plt <- update(plt, main = paste("Variogram for", as.character(a[i])))

   return(plt)
})

for (i in seq(names(seoul311_splt))) {
   png(paste0("plot_", names(seoul311_splt)[i], ".png"),
       width = 25, height = 25, units = "cm", res = 300)
   print(vars[[i]])
   dev.off()
}


HTH
Tim

On 27.08.2015 12:25, Uzzal wrote:
>
> Dear Tim, after getting lots of help from you, I was trying to 
> plot all variogram in different plot with different name (1 variogram 
> per plot). I was trying to naming those variogram like "variogram for 
> 20120301 01:00", "variogram for 20120301 02:00".......etc. 
> I improvised your code in the following way (below) but didn't get 
> success! Is it possible to plot different variogram in different plot 
> with different name  and save it in a single loop?  Thanks a lot for 
> your valuable time.
>
> a<-as.POSIXct(names(seoul311_split), format="%Y%m%d%H")
> a
> hours<-substr(a,1,16)
> hours
>
> vars<-lapply(seq(seoul311_split), function(i)
> {
>   dat<-seoul311_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,
>               main= paste("Semivariogram for", hours[i]))
>
>   return(plot)
> })
>
> Uzzal
>
>     --- Original Message ---
>     *From : * "Uzzal"<uzzal at gist.ac.kr>
>     *To : * "Tim Appelhans"<tim.appelhans at gmail.com>,
>     "r-sig-geo"<r-sig-geo at r-project.org>
>     *Date : * 2015/08/27 Thursday AM 1:44:12
>     *Subject : * Re: Re: [R-sig-Geo] How to plot multiple
>     semi-variogram from a single dataset efficiently in R?
>
>     Thank you vary much. Now its totally working!
>
>     Uzzal
>
>         --- Original Message ---
>         *From : * "Tim Appelhans"<tim.appelhans at gmail.com>
>         *To : * "Uzzal"<uzzal at gist.ac.kr>,
>         "r-sig-geo"<r-sig-geo at r-project.org>
>         *Date : * 2015/08/26 Wednesday PM 5:22:39
>         *Subject : * Re: [R-sig-Geo] How to plot multiple
>         semi-variogram from a single dataset efficiently in R?
>
>         Uzzal,
>         first of all, please always reply to the list as well. As I
>         said, other people might have similar problems and will thus
>         be able to find a soultion in the mail archives.
>
>         If you run the code I gave you, you will see that it works.
>
>         The relevant bit is
>
>         layer(panel.text(x = 1000, y = 900, adj = c(0, 1),
>                              labels = dates[panel.number()]))
>
>         where the text gets plotted at x = 1000 and y = 900. In your
>         case, y = 900 is beyond the limits of the y-scale.
>
>         Replacing the above with this should fix it:
>
>         layer(grid.text(x = unit(0.1, "npc"),
>                             y = unit(0.9, "npc"),
>                             just = c("left", "top"),
>                             label = dates[panel.number()]))
>
>         This will use normalised x and y scales ranging from 0 to 1 no
>         matter what the absolute units are.
>
>         HTH
>         Tim
>
>         On 26.08.2015 10:12, Uzzal wrote:
>>
>>         Oh really! But I got plot like this(Attached)!
>>
>>         Uzzal
>>
>>             --- Original Message ---
>>             *From : * "Tim Appelhans"<tim.appelhans at gmail.com>
>>             *To : * "Uzzal"<uzzal at gist.ac.kr>,
>>             "r-sig-geo"<r-sig-geo at r-project.org>
>>             *Date : * 2015/08/26 Wednesday PM 4:41:28
>>             *Subject : * Re: [R-sig-Geo] How to plot multiple
>>             semi-variogram from a single dataset efficiently in R?
>>
>>             Uzzal,
>>             it should show exactly that in the top left corner of
>>             each variogram. It does on my computer.
>>
>>             Also, I include R-si-geo again as other people might have
>>             similar problems.
>>
>>             Cheers
>>             Tim
>>
>>             On 26.08.2015 08:06, Uzzal wrote:
>>>             I am extreamly sorry for my previous mail. Now its
>>>             working.I forgot to loaded "latticeExtra" package.
>>>
>>>             The plots I got ,it all have individual names . But
>>>             every plot has 7 variogram.is it possible to naming each
>>>             variogram in each plot also like
>>>             "20120301","20120302",..........."201203007"?
>>>
>>>             Uzzal
>>>
>>>                 --- Original Message ---
>>>                 *From : * "Tim Appelhans"<tim.appelhans at gmail.com>
>>>                 *To : * "Uzzal"<uzzal at gist.ac.kr>
>>>                 *Date : * 2015/08/26 Wednesday AM 1:14:25
>>>                 *Subject : * Re: [R-sig-Geo] How to plot multiple
>>>                 semi-variogram from a single dataset efficiently in R?
>>>
>>>                 How's this:
>>>
>>>                 ### in order to use latticeCombineGrid() you need to
>>>                 use ##################
>>>                 ### library(devtools) to install library(Rsenal)
>>>                 ##########################
>>>                 # install.packages(devtools)
>>>                 # library(devtools)
>>>                 #
>>>                 install_github("environmentalinformatics-marburg/Rsenal")
>>>                 ###########################################################################
>>>
>>>                 library(sp)
>>>                 library(gstat)
>>>                 library(rgdal)
>>>                 library(automap)
>>>                 library(Rsenal)
>>>                 library(latticeExtra)
>>>
>>>                 seoul311 <- read.csv("Downloads/seoul1to7.csv")
>>>                 seoul311 <- na.omit(seoul311)
>>>
>>>                 ### first we split seoul311 by time into a list
>>>                 rather than subsetting manually
>>>                 seoul311_splt <- split(seoul311, seoul311$time)
>>>
>>>                 ### now we loop (using lapply()) over each
>>>                 seoul311_splt entry and calculate
>>>                 ### variogram using autofitVariogram and return the
>>>                 variogram plot
>>>                 vars <- lapply(seq(seoul311_splt), function(i) {
>>>
>>>                   dat <- seoul311_splt[[i]]
>>>                   coordinates(dat) <- ~LON+LAT
>>>                   proj4string(dat) <- "+proj=longlat +datum=WGS84"
>>>                   dat <- spTransform(dat, CRS("+proj=utm +north
>>>                 +zone=52 +datum=WGS84"))
>>>
>>>                   variogram <- autofitVariogram(PM10 ~ 1, dat)
>>>
>>>                   plt <- plot(variogram, plotit = FALSE, asp = 1)
>>>
>>>                   ### in case you do not want to fix xlim and ylim
>>>                 to be identical
>>>                   ### for each plot just comment out the following
>>>                 line or change
>>>                   ### values as you see fit
>>>                   plt <- update(plt, xlim = c(-1000, 45000), ylim =
>>>                 c(0, 1000))
>>>
>>>                   return(plt)
>>>                 })
>>>
>>>                 ### now we actually have 23 * 7 variogram plots
>>>                 which we will combine
>>>                 ### into 23 hourly plots using latticeCombineGrid()
>>>                 hrs <- substr(names(seoul311_splt), 9, 10)
>>>                 dates <- unique(substr(names(seoul311_splt), 1, 8))
>>>                 hrs_unique <- unique(substr(names(seoul311_splt), 9,
>>>                 10))
>>>
>>>                 plts_hrs <- lapply(seq(unique(hrs)), function(j) {
>>>
>>>                   indx <- hrs %in% unique(hrs)[j]
>>>                   hr_plt <- vars[indx]
>>>
>>>                   fin_plt <- latticeCombineGrid(hr_plt, layout =
>>>                 c(3, 3)) +
>>>                     layer(panel.text(x = 1000, y = 900, adj = c(0, 1),
>>>                 labels = dates[panel.number()]))
>>>
>>>                   fin_plt <- update(fin_plt, main = paste("Variogram
>>>                 for hour", hrs_unique[j]))
>>>
>>>                   return(fin_plt)
>>>
>>>                 })
>>>
>>>                 ### save each plot as .png
>>>                 ### NOTE: latttice plots need to be print()ed!!!
>>>                 for (i in seq(hrs_unique)) {
>>>                 png(paste0("plot_hr_", hrs_unique[i], ".png"),
>>>                       width = 25, height = 25, units = "cm", res = 300)
>>>                   print(plts_hrs[[i]])
>>>                   dev.off()
>>>                 }
>>>
>>>
>>>                 Is this what you need?
>>>
>>>                 Best
>>>                 Tim
>>>
>>>                 On 25.08.2015 06:19, Uzzal wrote:
>>>>
>>>>                 Hello Tim, How about your last weekend? By the way,
>>>>                 you told me to make you remember about my last
>>>>                 problem. Good day.
>>>>
>>>>                 Uzzal
>>>>
>>>>                     --- Original Message ---
>>>>                     *From : * "Tim Appelhans"<tim.appelhans at gmail.com>
>>>>                     *To : * "Uzzal"<uzzal at gist.ac.kr>
>>>>                     *Date : * 2015/08/19 Wednesday PM 7:21:54
>>>>                     *Subject : * Re: [R-sig-Geo] How to plot
>>>>                     multiple semi-variogram from a single dataset
>>>>                     efficiently in R?
>>>>
>>>>                     Uzzal,
>>>>                     this is surely possible. I will be on holidays
>>>>                     until next week so will not get back to you
>>>>                     before then.
>>>>                     If you haven't heard from me by the end of next
>>>>                     week, please send me a reminder email as I will
>>>>                     most likely have gotten crried away with other
>>>>                     things.
>>>>
>>>>                     Cheers
>>>>                     Tim
>>>>
>>>>                     On 18.08.2015 19:03, Uzzal wrote:
>>>>>
>>>>>                     Dear Tim, Thank you so much for your quick
>>>>>                     response. It helped me a lot. I can't resist
>>>>>                     myself to ask you my another query related to
>>>>>                     this. I don't know, If you mind then please
>>>>>                     tell me, I will ask in another email:
>>>>>
>>>>>                     My another query:
>>>>>                     According to your coad,
>>>>>
>>>>>
>>>>>                     plts_hrs[[1]]
>>>>>                         .
>>>>>                         .
>>>>>                     plts_hrs[[23]] give me the 23 plots
>>>>>                     individually with same title. If I want those
>>>>>                     23 plots _with different title(also individual
>>>>>                     name of variogram in a plot)_
>>>>>
>>>>>                      by a single loop and those plots will be
>>>>>                     automatically saved in a folder, Is it
>>>>>                     possible in R?
>>>>>
>>>>>                     For example, First plot title would be
>>>>>                     "Variogram for 1.00 a.m
>>>>>
>>>>>                     And the 7 variograms title in the first plot
>>>>>                     would be like 1 march , 2.00 march....7 march.
>>>>>
>>>>>                     Actually I am new in R and weak in loop operation.
>>>>>
>>>>>                     I tried to do it with dev.off () command. But
>>>>>                     I didn't get success.
>>>>>                     Could you please help me? Thanks a lot again.
>>>>>
>>>>>                     Orpheus
>>>>>
>>>>>                         --- Original Message ---
>>>>>                         *From : * "Tim
>>>>>                         Appelhans"<tim.appelhans at gmail.com>
>>>>>                         *To : * r-sig-geo at r-project.org
>>>>>                         *Cc : * uzzal at gist.ac.kr
>>>>>                         *Date : * 2015/08/17 Monday PM 6:49:00
>>>>>                         *Subject : * Re: [R-sig-Geo] How to plot
>>>>>                         multiple semi-variogram from a single
>>>>>                         dataset efficiently in R?
>>>>>
>>>>>                         Orpheus,
>>>>>                         here's one option to get what you want.
>>>>>                         The problem with fitting that many
>>>>>                         variograms is that if you want to specify
>>>>>                         each variogram individually (i.e. nugget,
>>>>>                         range, model etc.), there is no way to
>>>>>                         automate this with loops or thelike.
>>>>>                         You can, however, use library(automap) to
>>>>>                         automatically fit a variogram to your
>>>>>                         data. Then a possible solution could look
>>>>>                         like this:
>>>>>
>>>>>
>>>>>                         ### in order to use latticeCombineGrid()
>>>>>                         you need to use ##################
>>>>>                         ### library(devtools) to install
>>>>>                         library(Rsenal) ##########################
>>>>>                         # install.packages(devtools)
>>>>>                         # library(devtools)
>>>>>                         #
>>>>>                         install_github("environmentalinformatics-marburg/Rsenal")
>>>>>                         ###########################################################################
>>>>>
>>>>>                         library(sp)
>>>>>                         library(gstat)
>>>>>                         library(rgdal)
>>>>>                         library(automap)
>>>>>                         library(Rsenal)
>>>>>
>>>>>                         seoul311 <-
>>>>>                         read.csv("Downloads/seoul1to7.csv")
>>>>>                         seoul311 <- na.omit(seoul311)
>>>>>
>>>>>                         ### first we split seoul311 by time into a
>>>>>                         list rather than subsetting manually
>>>>>                         seoul311_splt <- split(seoul311,
>>>>>                         seoul311$time)
>>>>>
>>>>>                         ### now we loop (using lapply()) over each
>>>>>                         seoul311_splt entry and calculate
>>>>>                         ### variogram using autofitVariogram and
>>>>>                         return the variogram plot
>>>>>                         vars <- lapply(seq(seoul311_splt),
>>>>>                         function(i) {
>>>>>
>>>>>                           dat <- seoul311_splt[[i]]
>>>>>                         coordinates(dat) <- ~LON+LAT
>>>>>                         proj4string(dat) <- "+proj=longlat
>>>>>                         +datum=WGS84"
>>>>>                           dat <- spTransform(dat, CRS("+proj=utm
>>>>>                         +north +zone=52 +datum=WGS84"))
>>>>>
>>>>>                           variogram <- autofitVariogram(PM10 ~ 1, dat)
>>>>>
>>>>>                           plt <- plot(variogram, plotit = FALSE,
>>>>>                         asp = 1)
>>>>>
>>>>>                           ### in case you do not want to fix xlim
>>>>>                         and ylim to be identical
>>>>>                           ### for each plot just comment out the
>>>>>                         following line or change
>>>>>                           ### values as you see fit
>>>>>                           plt <- update(plt, xlim = c(-1000,
>>>>>                         45000), ylim = c(0, 1000))
>>>>>
>>>>>                           return(plt)
>>>>>                         })
>>>>>
>>>>>                         ### now we actually have 23 * 7 variogram
>>>>>                         plots which we will combine
>>>>>                         ### into 23 hourly plots using
>>>>>                         latticeCombineGrid()
>>>>>                         hrs <- substr(names(seoul311_splt), 9, 10)
>>>>>
>>>>>                         plts_hrs <- lapply(seq(unique(hrs)),
>>>>>                         function(j) {
>>>>>
>>>>>                           indx <- hrs %in% unique(hrs)[j]
>>>>>                           hr_plt <- vars[indx]
>>>>>
>>>>>                         return(latticeCombineGrid(hr_plt, layout =
>>>>>                         c(3, 3)))
>>>>>
>>>>>                         })
>>>>>
>>>>>                         ### plot for hour 1
>>>>>                         plts_hrs[[1]]
>>>>>
>>>>>                         ### plot for hour 23
>>>>>                         plts_hrs[[23]]
>>>>>
>>>>>                         ### ...
>>>>>
>>>>>
>>>>>                         HTH
>>>>>
>>>>>                         Best,
>>>>>                         Tim
>>>>>
>>>>>
>>>>>                         On 17.08.2015 09:43, Uzzal wrote:
>>>>>>                         Actually I asked this same question to
>>>>>>                         some other forum but unfortunate I didn't
>>>>>>                         get the appropriate response. But I am
>>>>>>                         kind of struck with this problem. I am
>>>>>>                         giving the link of my problem previously
>>>>>>                         posted in other forum: (really sorry for
>>>>>>                         that)
>>>>>>                         http://stackoverflow.com/questions/31762461/how-to-plot-multiple-semi-variogram-from-a-single-dataset-efficiently-in-r?noredirect=1#comment51767989_31762461
>>>>>>
>>>>>>                         I want variogram time wise. For example,
>>>>>>                         thevariogram for 1.00 am of 1 march to 7
>>>>>>                         march in one plot. That means 7
>>>>>>                         variograms in one plot. in such a way
>>>>>>                         there will be 23 plot contains (23*7)
>>>>>>                         variogram. Thanks in adv! ance.
>>>>>>
>>>>>>                         Orpheus
>>>>>>
>>>>>>
>>>>>>
>>>>>>                         Uzzal Kumar Dash
>>>>>>                         Master's student
>>>>>>                         Air Quality & Climate Modeling Laboratory
>>>>>>                         (AqCliMB)
>>>>>>                         School of Environmental Science and
>>>>>>                         Engineering (SESE)
>>>>>>                         Gwangju Institute of Science and
>>>>>>                         Technology (GIST)
>>>>>>                         123 Cheomdan-gwagiro, Buk-gu, Gwangju 500-712
>>>>>>                         Republic of Korea
>>>>>>
>>>>>>                         web: http://aqclimb.gist.ac.kr/
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>                         _______________________________________________
>>>>>>                         R-sig-Geo mailing list
>>>>>>                         R-sig-Geo at r-project.org
>>>>>>                         https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>
>>>>>                         -- 
>>>>>                         #####################################
>>>>>                         Tim Appelhans
>>>>>                         Department of Geography
>>>>>                         Environmental Informatics
>>>>>                         Philipps Universit?t Marburg
>>>>>                         Deutschhausstra?e 12
>>>>>                         35032 Marburg (Paketpost: 35037 Marburg)
>>>>>                         Germany
>>>>>
>>>>>                         Tel +49 (0) 6421 28-25957
>>>>>
>>>>>                         http://environmentalinformatics-marburg.de/
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>                     Uzzal Kumar Dash
>>>>>                     Master's student
>>>>>                     Air Quality & Climate Modeling Laboratory
>>>>>                     (AqCliMB)
>>>>>                     School of Environmental Science and
>>>>>                     Engineering (SESE)
>>>>>                     Gwangju Institute of Science and Technology (GIST)
>>>>>                     123 Cheomdan-gwagiro, Buk-gu, Gwangju 500-712
>>>>>                     Republic of Korea
>>>>>
>>>>>                     web: http://aqclimb.gist.ac.kr/
>>>>>
>>>>>
>>>>
>>>>                     -- 
>>>>                     #####################################
>>>>                     Tim Appelhans
>>>>                     Department of Geography
>>>>                     Environmental Informatics
>>>>                     Philipps Universit?t Marburg
>>>>                     Deutschhausstra?e 12
>>>>                     35032 Marburg (Paketpost: 35037 Marburg)
>>>>                     Germany
>>>>
>>>>                     Tel +49 (0) 6421 28-25957
>>>>
>>>>                     http://environmentalinformatics-marburg.de/
>>>>
>>>>
>>>>
>>>>
>>>>                 Uzzal Kumar Dash
>>>>                 Master's student
>>>>                 Air Quality & Climate Modeling Laboratory (AqCliMB)
>>>>                 School of Environmental Science and Engineering (SESE)
>>>>                 Gwangju Institute of Science and Technology (GIST)
>>>>                 123 Cheomdan-gwagiro, Buk-gu, Gwangju 500-712
>>>>                 Republic of Korea
>>>>
>>>>                 web: http://aqclimb.gist.ac.kr/
>>>>
>>>>
>>>
>>>                 -- 
>>>                 #####################################
>>>                 Tim Appelhans
>>>                 Department of Geography
>>>                 Environmental Informatics
>>>                 Philipps Universit?t Marburg
>>>                 Deutschhausstra?e 12
>>>                 35032 Marburg (Paketpost: 35037 Marburg)
>>>                 Germany
>>>
>>>                 Tel +49 (0) 6421 28-25957
>>>
>>>                 http://environmentalinformatics-marburg.de/
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>             -- 
>>             #####################################
>>             Tim Appelhans
>>             Department of Geography
>>             Environmental Informatics
>>             Philipps Universit?t Marburg
>>             Deutschhausstra?e 12
>>             35032 Marburg (Paketpost: 35037 Marburg)
>>             Germany
>>
>>             Tel +49 (0) 6421 28-25957
>>
>>             http://environmentalinformatics-marburg.de/
>>
>>
>>
>>
>>
>>
>
>         -- 
>         #####################################
>         Tim Appelhans
>         Department of Geography
>         Environmental Informatics
>         Philipps Universit?t Marburg
>         Deutschhausstra?e 12
>         35032 Marburg (Paketpost: 35037 Marburg)
>         Germany
>
>         Tel +49 (0) 6421 28-25957
>
>         http://environmentalinformatics-marburg.de/
>
>
>
>
>
>
>
>
>
>
>

-- 
#####################################
Tim Appelhans
Department of Geography
Environmental Informatics
Philipps Universität Marburg
Deutschhausstraße 12
35032 Marburg (Paketpost: 35037 Marburg)
Germany

Tel +49 (0) 6421 28-25957

http://environmentalinformatics-marburg.de/


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list