Dear all, I'd like to change a point pattern to a grid of cells and use one of the variables as the output. e.g. The point pattern is of a window of (500*500) and several features such as pH, SoilType etc. I like to divide it into a grid with cell size 5*5, and use the mean of the point values falling inside the cell as the output. Is there any package in R working with this? Thanks in advance! Cheers, Leaf
On Thu, 17 Nov 2005, Leaf Sun wrote:> Dear all, > > I'd like to change a point pattern to a grid of cells and use one of the > variables as the output. > > e.g. The point pattern is of a window of (500*500) and several features > such as pH, SoilType etc. I like to divide it into a grid with cell > size 5*5, and use the mean of the point values falling inside the cell > as the output. > > Is there any package in R working with this? Thanks in advance!This might have been better posted on R-sig-geo. Try this: library(sp) df1 <- data.frame(x=runif(10000,0,500), y=runif(10000,0,500), z=rnorm(10000)) coordinates(df1) <- c("x", "y") summary(df1) # SpatialPointsDataFrame grd <- GridTopology(c(2.5,2.5), c(5,5), c(100,100)) sgrd <- SpatialGrid(grd) #SpatialGrid bbox(sgrd) res <- overlay(sgrd, df1) # find which grid cells the points are in str(res) try0 <- lapply(split(as(df1, "data.frame"), res), mean) # take means by grid cell - assumes all numeric columns in df1 # (soil type??) - maybe write a custom function to handle non-numeric # columns sensibly try01 <- vector(mode="list", length=prod(slot(slot(sgrd, "grid"), "cells.dim"))) nafill <- rep(as.numeric(NA), ncol(as(df1, "data.frame"))) try01 <- lapply(try01, function(x) nafill) # make a container to put the means in with the right number of columns try01[as.integer(names(try0))] <- try0 # insert means into correct list elements try1 <- data.frame(t(data.frame(try01))) # transpose summary(try1) sgrd1 <- SpatialGridDataFrame(slot(sgrd, "grid"), try1) image(sgrd1, "x") image(sgrd1, "y") image(sgrd1, "z") It goes a bit further than the short description of the sp package in the latest R-News, and will most likely be a new method for overlay in sp. If these are your 200K points, it may take a little longer ...> > Cheers, > > Leaf > >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Hi Roger, Thanks again for your kind help. Yes, I still use the 200K points data applying this program but the good thing is I found it finished in no time. The questions again here are: 1) >try0 <- lapply(split(as(df1, "data.frame"), res), mean) When I tried to replace mean to sum, error looks like this: Error in x at data[, i, drop = FALSE] : undefined columns selected 2) If I just need to know the number of points in each cells, how can I modify the codes. The codes still a bit beyond me. Thanks! Leaf ======= At 2005-11-18, 01:39:05 you wrote: ======>On Thu, 17 Nov 2005, Leaf Sun wrote: > >> Dear all, >> >> I'd like to change a point pattern to a grid of cells and use one of the >> variables as the output. >> >> e.g. The point pattern is of a window of (500*500) and several features >> such as pH, SoilType etc. I like to divide it into a grid with cell >> size 5*5, and use the mean of the point values falling inside the cell >> as the output. >> >> Is there any package in R working with this? Thanks in advance! > >This might have been better posted on R-sig-geo. Try this: > >library(sp) >df1 <- data.frame(x=runif(10000,0,500), y=runif(10000,0,500), > z=rnorm(10000)) >coordinates(df1) <- c("x", "y") >summary(df1) # SpatialPointsDataFrame >grd <- GridTopology(c(2.5,2.5), c(5,5), c(100,100)) >sgrd <- SpatialGrid(grd) #SpatialGrid >bbox(sgrd) >res <- overlay(sgrd, df1) ># find which grid cells the points are in >str(res) >try0 <- lapply(split(as(df1, "data.frame"), res), mean) ># take means by grid cell - assumes all numeric columns in df1 ># (soil type??) - maybe write a custom function to handle non-numeric ># columns sensibly >try01 <- vector(mode="list", length=prod(slot(slot(sgrd, "grid"), > "cells.dim"))) >nafill <- rep(as.numeric(NA), ncol(as(df1, "data.frame"))) >try01 <- lapply(try01, function(x) nafill) ># make a container to put the means in with the right number of columns >try01[as.integer(names(try0))] <- try0 ># insert means into correct list elements >try1 <- data.frame(t(data.frame(try01))) ># transpose >summary(try1) >sgrd1 <- SpatialGridDataFrame(slot(sgrd, "grid"), try1) >image(sgrd1, "x") >image(sgrd1, "y") >image(sgrd1, "z") > >It goes a bit further than the short description of the sp package in the >latest R-News, and will most likely be a new method for overlay in sp. If >these are your 200K points, it may take a little longer ... > >> >> Cheers, >> >> Leaf >> >> > >-- >Roger Bivand >Economic Geography Section, Department of Economics, Norwegian School of >Economics and Business Administration, Helleveien 30, N-5045 Bergen, >Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 >e-mail: Roger.Bivand at nhh.no >= = = = = = = = = = = = = = = = = = = =
Hi all, I am new in R tool. I have a basic question. I would like to do a combination of two multiple lines plots for one X-variable and two different sets (lists) of Y-variables. Any suggestion will be greatly appreciated. Thanks! Wei
?lines ?matplot ?ts.plot library(zoo); ?plot.zoo On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote:> Hi all, > > I am new in R tool. I have a basic question. I would like to do a > combination of two multiple lines plots for one X-variable and two > different sets (lists) of Y-variables. Any suggestion will be greatly > appreciated. > > Thanks! > > Wei > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Dear Babor and all, Thanks for your quick response. I tried the following. We can plot y1 and Y2 on one figs. It just shows one Y axis on the left side. I would like to show the Y1 axis on left side and Y2 Axis on right side. Any others input, Wei On Sat, 26 Nov 2005, Gabor Grothendieck wrote:> ?lines > ?matplot > ?ts.plot > library(zoo); ?plot.zoo > > > On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote: >> Hi all, >> >> I am new in R tool. I have a basic question. I would like to do a >> combination of two multiple lines plots for one X-variable and two >> different sets (lists) of Y-variables. Any suggestion will be greatly >> appreciated. >> >> Thanks! >> >> Wei >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >> > > >
On Sat, 26 Nov 2005, Wei Qiu wrote:> Dear Gabor and all, > > Thanks for your quick response. I tried the following. We can plot > y1 and Y2 on one figs. It just shows one Y axis on the left side. I > would like to show the Y1 axis on left side and Y2 Axis on right side. > > Any others input, > > Wei > > On Sat, 26 Nov 2005, Gabor Grothendieck wrote: > >> ?lines >> ?matplot >> ?ts.plot >> library(zoo); ?plot.zoo >> >> >> On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote: >>> Hi all, >>> >>> I am new in R tool. I have a basic question. I would like to do a >>> combination of two multiple lines plots for one X-variable and two >>> different sets (lists) of Y-variables. Any suggestion will be greatly >>> appreciated. >>> >>> Thanks! >>> >>> Wei >>> >>> ______________________________________________ >>> R-help at stat.math.ethz.ch mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >>> >> >> >> > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >
axis(4, .,..) does it. Try RSiteSearch("axis(4,") to locate examples. On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote:> On Sat, 26 Nov 2005, Wei Qiu wrote: > > > Dear Gabor and all, > > > > Thanks for your quick response. I tried the following. We can plot > > y1 and Y2 on one figs. It just shows one Y axis on the left side. I > > would like to show the Y1 axis on left side and Y2 Axis on right side. > > > > Any others input, > > > > Wei > > > > On Sat, 26 Nov 2005, Gabor Grothendieck wrote: > > > >> ?lines > >> ?matplot > >> ?ts.plot > >> library(zoo); ?plot.zoo > >> > >> > >> On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote: > >>> Hi all, > >>> > >>> I am new in R tool. I have a basic question. I would like to do a > >>> combination of two multiple lines plots for one X-variable and two > >>> different sets (lists) of Y-variables. Any suggestion will be greatly > >>> appreciated. > >>> > >>> Thanks! > >>> > >>> Wei > >>> > >>> ______________________________________________ > >>> R-help at stat.math.ethz.ch mailing list > >>> https://stat.ethz.ch/mailman/listinfo/r-help > >>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >>> > >> > >> > >> > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > > > >
On 26 Nov 2005 at 15:40, Wei Qiu wrote: Date sent: Sat, 26 Nov 2005 15:40:52 -0500 (EST) From: Wei Qiu <wqiu at nmr.mgh.harvard.edu> To: Gabor Grothendieck <ggrothendieck at gmail.com> Copies to: r-help <r-help at stat.math.ethz.ch> Subject: Re: [R] How to do the multiple plots?> Dear Babor and all, > > Thanks for your quick response. I tried the following. We can plot y1 > and Y2 on one figs. It just shows one Y axis on the left side. I would > like to show the Y1 axis on left side and Y2 Axis on right side. > > Any others input,Hi so add to your evening reading also ?axis and other functions from See also chapters in man pages. HTH Petr> > Wei > > On Sat, 26 Nov 2005, Gabor Grothendieck wrote: > > > ?lines > > ?matplot > > ?ts.plot > > library(zoo); ?plot.zoo > > > > > > On 11/26/05, Wei Qiu <wqiu at nmr.mgh.harvard.edu> wrote: > >> Hi all, > >> > >> I am new in R tool. I have a basic question. I would like to do a > >> combination of two multiple lines plots for one X-variable and two > >> different sets (lists) of Y-variables. Any suggestion will be > >> greatly appreciated. > >> > >> Thanks! > >> > >> Wei > >> > >> ______________________________________________ > >> R-help at stat.math.ethz.ch mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide! > >> http://www.R-project.org/posting-guide.html > >> > > > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz