|
I have a SpatialPointsDataFrame object in which many points are very close
together, such that the markers (plotting characters) tend to overlap. (Of course, this depends on marker size and the scale at which I plot; if I "zoom in" there is less overlap.) It appears that when markers overlap, spplot() places a marker associated with the larger value after, and therefore on top of, a marker associated with a smaller value. Am I correct? Or more generally, what is the algorithm that determines the order in which markers are added? I have searched ?spplot and related help pages and haven't found an explanation (at least, not yet). I can add that I don't believe markers are placed in the order in which they appear in the SpatialPointsDataFrame. I say this because when I attempt to reproduce an spplot using base graphics plot() I have to sort from smallest to largest value to succeed. I can probably provide a small reproducible example if necessary, but I'm hoping it's not necessary. Here are my actual commands. tmps is the SpatialPointsDataFrame. tmp is coordinates(tmps) (they have the same number of rows in the same order) Note that I'm using the cuts argument to break a continuous variable ('cpm2') into bins. I have carefully matched the colors in tmps$col2 with the cbin.cols object passed to spplot(), and the break points for the bin boundaries, so I believe that everything else that could affect the final appearance, other than the order in which the markers are placed, is controlled. #1 using spplot spplot(tmps,c('cpm2'), key.space='right', legendEntries=cbin.lbls, cuts=cbin.brks, col.regions=cbin.cols, cex=0.4) #2 using base graphics plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) Thanks -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
Don,
spplot essentially does this: df = data.frame(x=runif(100),y=runif(100),z=rnorm(100)) library(lattice) xyplot(y~x,groups=cut(df$z,5),df,pch=16,cex=5, col=grey((1:5)/6), asp="iso") for SpatialPointsDataFrame objects. So yes, it seems that the plotting order is determined by value. I guess that what you would have liked is the data order, i.e. xyplot(y~x,col=grey((1:5)/6)[cut(df$z,5)],df,pch=16,cex=5) and I think that is more reasonable to expect (and would allow for randomization). Best regards, On 03/02/2012 09:03 PM, MacQueen, Don wrote: > I have a SpatialPointsDataFrame object in which many points are very close > together, such that the markers (plotting characters) tend to overlap. (Of > course, this depends on marker size and the scale at which I plot; if I > "zoom in" there is less overlap.) > > It appears that when markers overlap, spplot() places a marker associated > with the larger value after, and therefore on top of, a marker associated > with a smaller value. > > Am I correct? Or more generally, what is the algorithm that determines the > order in which markers are added? > > > I have searched ?spplot and related help pages and haven't found an > explanation (at least, not yet). > > I can add that I don't believe markers are placed in the order in which > they appear in the SpatialPointsDataFrame. I say this because when I > attempt to reproduce an spplot using base graphics plot() I have to sort > from smallest to largest value to succeed. > > I can probably provide a small reproducible example if necessary, but I'm > hoping it's not necessary. > > Here are my actual commands. > > tmps is the SpatialPointsDataFrame. > tmp is coordinates(tmps) > (they have the same number of rows in the same order) > > Note that I'm using the cuts argument to break a continuous variable > ('cpm2') into bins. I have carefully matched the colors in tmps$col2 with > the cbin.cols object passed to spplot(), and the break points for the bin > boundaries, so I believe that everything else that could affect the final > appearance, other than the order in which the markers are placed, is > controlled. > > #1 using spplot > spplot(tmps,c('cpm2'), > key.space='right', > legendEntries=cbin.lbls, > cuts=cbin.brks, > col.regions=cbin.cols, > cex=0.4) > > #2 using base graphics > plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) > > > > Thanks > -Don > -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by MacQueen, Don
This has now been changed/repared in sp on r-forge (svn), and will
appear from sp 0.9-97 on. A test run would be: library(sp) xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) coordinates(xyz)=~x+y spplot(xyz, cex=10) which used to plot points in value order, and does now in data record order. I put up resulting graphs, for comparison, at http://ifgi.uni-muenster.de/~epebe_01/xyz.html As this change may make some of your spplot's for points different, if there are objections against this change (which I consider an improvement, if not a bug fix), it is time now to let me / us know. On 03/02/2012 09:03 PM, MacQueen, Don wrote: > I have a SpatialPointsDataFrame object in which many points are very close > together, such that the markers (plotting characters) tend to overlap. (Of > course, this depends on marker size and the scale at which I plot; if I > "zoom in" there is less overlap.) > > It appears that when markers overlap, spplot() places a marker associated > with the larger value after, and therefore on top of, a marker associated > with a smaller value. > > Am I correct? Or more generally, what is the algorithm that determines the > order in which markers are added? > > > I have searched ?spplot and related help pages and haven't found an > explanation (at least, not yet). > > I can add that I don't believe markers are placed in the order in which > they appear in the SpatialPointsDataFrame. I say this because when I > attempt to reproduce an spplot using base graphics plot() I have to sort > from smallest to largest value to succeed. > > I can probably provide a small reproducible example if necessary, but I'm > hoping it's not necessary. > > Here are my actual commands. > > tmps is the SpatialPointsDataFrame. > tmp is coordinates(tmps) > (they have the same number of rows in the same order) > > Note that I'm using the cuts argument to break a continuous variable > ('cpm2') into bins. I have carefully matched the colors in tmps$col2 with > the cbin.cols object passed to spplot(), and the break points for the bin > boundaries, so I believe that everything else that could affect the final > appearance, other than the order in which the markers are placed, is > controlled. > > #1 using spplot > spplot(tmps,c('cpm2'), > key.space='right', > legendEntries=cbin.lbls, > cuts=cbin.brks, > col.regions=cbin.cols, > cex=0.4) > > #2 using base graphics > plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) > > > > Thanks > -Don > -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
Edzer,
I think you've done the right thing -- and thank you for the quick response. Ironically, in the application that brought this up plotting higher values on top was convenient, because higher values are of more interest. However, I can still have higher values on top simply by sorting before plotting. The user now has more control, and this is good. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/5/12 5:40 AM, "Edzer Pebesma" <[hidden email]> wrote: >This has now been changed/repared in sp on r-forge (svn), and will >appear from sp 0.9-97 on. A test run would be: > >library(sp) >xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) >coordinates(xyz)=~x+y >spplot(xyz, cex=10) > >which used to plot points in value order, and does now in data record >order. > >I put up resulting graphs, for comparison, at >http://ifgi.uni-muenster.de/~epebe_01/xyz.html > >As this change may make some of your spplot's for points different, if >there are objections against this change (which I consider an >improvement, if not a bug fix), it is time now to let me / us know. > >On 03/02/2012 09:03 PM, MacQueen, Don wrote: >> I have a SpatialPointsDataFrame object in which many points are very >>close >> together, such that the markers (plotting characters) tend to overlap. >>(Of >> course, this depends on marker size and the scale at which I plot; if I >> "zoom in" there is less overlap.) >> >> It appears that when markers overlap, spplot() places a marker >>associated >> with the larger value after, and therefore on top of, a marker >>associated >> with a smaller value. >> >> Am I correct? Or more generally, what is the algorithm that determines >>the >> order in which markers are added? >> >> >> I have searched ?spplot and related help pages and haven't found an >> explanation (at least, not yet). >> >> I can add that I don't believe markers are placed in the order in which >> they appear in the SpatialPointsDataFrame. I say this because when I >> attempt to reproduce an spplot using base graphics plot() I have to sort >> from smallest to largest value to succeed. >> >> I can probably provide a small reproducible example if necessary, but >>I'm >> hoping it's not necessary. >> >> Here are my actual commands. >> >> tmps is the SpatialPointsDataFrame. >> tmp is coordinates(tmps) >> (they have the same number of rows in the same order) >> >> Note that I'm using the cuts argument to break a continuous variable >> ('cpm2') into bins. I have carefully matched the colors in tmps$col2 >>with >> the cbin.cols object passed to spplot(), and the break points for the >>bin >> boundaries, so I believe that everything else that could affect the >>final >> appearance, other than the order in which the markers are placed, is >> controlled. >> >> #1 using spplot >> spplot(tmps,c('cpm2'), >> key.space='right', >> legendEntries=cbin.lbls, >> cuts=cbin.brks, >> col.regions=cbin.cols, >> cex=0.4) >> >> #2 using base graphics >> plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) >> >> >> >> Thanks >> -Don >> > >-- >Edzer Pebesma >Institute for Geoinformatics (ifgi), University of Münster >Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 >8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de >http://www.52north.org/geostatistics [hidden email] > >_______________________________________________ >R-sig-Geo mailing list >[hidden email] >https://stat.ethz.ch/mailman/listinfo/r-sig-geo _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
On 03/05/2012 04:36 PM, MacQueen, Don wrote: > Edzer, > > I think you've done the right thing -- and thank you for the quick > response. > > Ironically, in the application that brought this up plotting higher values > on top was convenient, because higher values are of more interest. > However, I can still have higher values on top simply by sorting before > plotting. The user now has more control, and this is good. Right, the old behaviour is obtained by reordering the data, as in: library(sp) xyz = data.frame(expand.grid(x=1:10,y=1:10), z = rnorm(100)) coordinates(xyz)=~x+y spplot(xyz[order(xyz$z),], cex=10) -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
Is there anyway to calculate population weighted centroids of a spatial
polygon in R? Any idea will be appreciated. Thanks Zia -- --------------------- Zia Ahmed, PhD Research Associate Department of Crop and Soil Sciences 1002 Bradfield Hall, Cornell University Ithaca, NY 14853-4203 t. 607.255.9387 f. 607.255.3207 email [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
On Tue, Mar 6, 2012 at 1:49 AM, Zia Ahmed <[hidden email]> wrote:
> Is there anyway to calculate population weighted centroids of a spatial > polygon in R? Any idea will be appreciated. Thanks Some more clues would be appreciated! What's your data? A set of points representing cities with populations and an additional set of polygons? Or a raster population density and a set of polygons? Or something else? Either way it can be done, but unless you tell us more precisely what your problem is we'll all be wasting our time! Barry _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by Edzer Pebesma-2
On 05/03/12 14:40, Edzer Pebesma wrote:
> This has now been changed/repared in sp on r-forge (svn), and will > appear from sp 0.9-97 on. A test run would be: > > library(sp) > xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) > coordinates(xyz)=~x+y > spplot(xyz, cex=10) > > which used to plot points in value order, and does now in data record order. > > I put up resulting graphs, for comparison, at > http://ifgi.uni-muenster.de/~epebe_01/xyz.html > > As this change may make some of your spplot's for points different, if > there are objections against this change (which I consider an > improvement, if not a bug fix), it is time now to let me / us know. Would it be possible to set a degree of transparency for the overlapping points in spplot ? Thanks in advance, Mauricio Zambrano-Bigiarini - ======================================================= FLOODS Action Water Resources Unit (H01) Institute for Environment and Sustainability (IES) European Commission, Joint Research Centre (JRC) webinfo : http://floods.jrc.ec.europa.eu/ ======================================================= DISCLAIMER: "The views expressed are purely those of the writer and may not in any circumstances be regarded as stating an official position of the European Commission." ======================================================= Linux user #454569 -- Ubuntu user #17469 ======================================================= "If Columbus had turned back, no one would have blamed him. Of course, no one would have remembered him either." (Source Unknown) > > On 03/02/2012 09:03 PM, MacQueen, Don wrote: >> I have a SpatialPointsDataFrame object in which many points are very close >> together, such that the markers (plotting characters) tend to overlap. (Of >> course, this depends on marker size and the scale at which I plot; if I >> "zoom in" there is less overlap.) >> >> It appears that when markers overlap, spplot() places a marker associated >> with the larger value after, and therefore on top of, a marker associated >> with a smaller value. >> >> Am I correct? Or more generally, what is the algorithm that determines the >> order in which markers are added? >> >> >> I have searched ?spplot and related help pages and haven't found an >> explanation (at least, not yet). >> >> I can add that I don't believe markers are placed in the order in which >> they appear in the SpatialPointsDataFrame. I say this because when I >> attempt to reproduce an spplot using base graphics plot() I have to sort >> from smallest to largest value to succeed. >> >> I can probably provide a small reproducible example if necessary, but I'm >> hoping it's not necessary. >> >> Here are my actual commands. >> >> tmps is the SpatialPointsDataFrame. >> tmp is coordinates(tmps) >> (they have the same number of rows in the same order) >> >> Note that I'm using the cuts argument to break a continuous variable >> ('cpm2') into bins. I have carefully matched the colors in tmps$col2 with >> the cbin.cols object passed to spplot(), and the break points for the bin >> boundaries, so I believe that everything else that could affect the final >> appearance, other than the order in which the markers are placed, is >> controlled. >> >> #1 using spplot >> spplot(tmps,c('cpm2'), >> key.space='right', >> legendEntries=cbin.lbls, >> cuts=cbin.brks, >> col.regions=cbin.cols, >> cex=0.4) >> >> #2 using base graphics >> plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) >> >> >> >> Thanks >> -Don >> > - _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
Yes, look for argument alpha e.g. in ?rgb or ?bpy.colors
On 03/06/2012 04:49 PM, Mauricio Zambrano-Bigiarini wrote: > On 05/03/12 14:40, Edzer Pebesma wrote: >> This has now been changed/repared in sp on r-forge (svn), and will >> appear from sp 0.9-97 on. A test run would be: >> >> library(sp) >> xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) >> coordinates(xyz)=~x+y >> spplot(xyz, cex=10) >> >> which used to plot points in value order, and does now in data record >> order. >> >> I put up resulting graphs, for comparison, at >> http://ifgi.uni-muenster.de/~epebe_01/xyz.html >> >> As this change may make some of your spplot's for points different, if >> there are objections against this change (which I consider an >> improvement, if not a bug fix), it is time now to let me / us know. > > Would it be possible to set a degree of transparency for the > overlapping points in spplot ? > > Thanks in advance, > > Mauricio Zambrano-Bigiarini > - > ======================================================= > FLOODS Action > Water Resources Unit (H01) > Institute for Environment and Sustainability (IES) > European Commission, Joint Research Centre (JRC) > webinfo : http://floods.jrc.ec.europa.eu/ > ======================================================= > DISCLAIMER: > "The views expressed are purely those of the writer > and may not in any circumstances be regarded as stating > an official position of the European Commission." > ======================================================= > Linux user #454569 -- Ubuntu user #17469 > ======================================================= > "If Columbus had turned back, no one would have blamed him. > Of course, no one would have remembered him either." > (Source Unknown) > > >> >> On 03/02/2012 09:03 PM, MacQueen, Don wrote: >>> I have a SpatialPointsDataFrame object in which many points are very >>> close >>> together, such that the markers (plotting characters) tend to >>> overlap. (Of >>> course, this depends on marker size and the scale at which I plot; if I >>> "zoom in" there is less overlap.) >>> >>> It appears that when markers overlap, spplot() places a marker >>> associated >>> with the larger value after, and therefore on top of, a marker >>> associated >>> with a smaller value. >>> >>> Am I correct? Or more generally, what is the algorithm that >>> determines the >>> order in which markers are added? >>> >>> >>> I have searched ?spplot and related help pages and haven't found an >>> explanation (at least, not yet). >>> >>> I can add that I don't believe markers are placed in the order in which >>> they appear in the SpatialPointsDataFrame. I say this because when I >>> attempt to reproduce an spplot using base graphics plot() I have to sort >>> from smallest to largest value to succeed. >>> >>> I can probably provide a small reproducible example if necessary, but >>> I'm >>> hoping it's not necessary. >>> >>> Here are my actual commands. >>> >>> tmps is the SpatialPointsDataFrame. >>> tmp is coordinates(tmps) >>> (they have the same number of rows in the same order) >>> >>> Note that I'm using the cuts argument to break a continuous variable >>> ('cpm2') into bins. I have carefully matched the colors in tmps$col2 >>> with >>> the cbin.cols object passed to spplot(), and the break points for the >>> bin >>> boundaries, so I believe that everything else that could affect the >>> final >>> appearance, other than the order in which the markers are placed, is >>> controlled. >>> >>> #1 using spplot >>> spplot(tmps,c('cpm2'), >>> key.space='right', >>> legendEntries=cbin.lbls, >>> cuts=cbin.brks, >>> col.regions=cbin.cols, >>> cex=0.4) >>> >>> #2 using base graphics >>> plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) >>> >>> >>> >>> Thanks >>> -Don >>> >> > > > - -- Edzer Pebesma Institute for Geoinformatics (ifgi), University of Münster Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics [hidden email] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
On 06/03/12 16:55, Edzer Pebesma wrote:
> Yes, look for argument alpha e.g. in ?rgb or ?bpy.colors Thank you very much Edzer. It works perfectly: library(sp) xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) coordinates(xyz)=~x+y spplot(xyz, cex=10, alpha=0.7) Cheers, Mauricio -- ======================================================= FLOODS Action Water Resources Unit (H01) Institute for Environment and Sustainability (IES) European Commission, Joint Research Centre (JRC) webinfo : http://floods.jrc.ec.europa.eu/ ======================================================= DISCLAIMER: "The views expressed are purely those of the writer and may not in any circumstances be regarded as stating an official position of the European Commission." ======================================================= Linux user #454569 -- Ubuntu user #17469 ======================================================= "If Columbus had turned back, no one would have blamed him. Of course, no one would have remembered him either." (Source Unknown) > > On 03/06/2012 04:49 PM, Mauricio Zambrano-Bigiarini wrote: >> On 05/03/12 14:40, Edzer Pebesma wrote: >>> This has now been changed/repared in sp on r-forge (svn), and will >>> appear from sp 0.9-97 on. A test run would be: >>> >>> library(sp) >>> xyz = data.frame(expand.grid(x=1:10,y=1:10),rnorm(100)) >>> coordinates(xyz)=~x+y >>> spplot(xyz, cex=10) >>> >>> which used to plot points in value order, and does now in data record >>> order. >>> >>> I put up resulting graphs, for comparison, at >>> http://ifgi.uni-muenster.de/~epebe_01/xyz.html >>> >>> As this change may make some of your spplot's for points different, if >>> there are objections against this change (which I consider an >>> improvement, if not a bug fix), it is time now to let me / us know. >> >> Would it be possible to set a degree of transparency for the >> overlapping points in spplot ? >> >> Thanks in advance, >> >> Mauricio Zambrano-Bigiarini >> - >> ======================================================= >> FLOODS Action >> Water Resources Unit (H01) >> Institute for Environment and Sustainability (IES) >> European Commission, Joint Research Centre (JRC) >> webinfo : http://floods.jrc.ec.europa.eu/ >> ======================================================= >> DISCLAIMER: >> "The views expressed are purely those of the writer >> and may not in any circumstances be regarded as stating >> an official position of the European Commission." >> ======================================================= >> Linux user #454569 -- Ubuntu user #17469 >> ======================================================= >> "If Columbus had turned back, no one would have blamed him. >> Of course, no one would have remembered him either." >> (Source Unknown) >> >> >>> >>> On 03/02/2012 09:03 PM, MacQueen, Don wrote: >>>> I have a SpatialPointsDataFrame object in which many points are very >>>> close >>>> together, such that the markers (plotting characters) tend to >>>> overlap. (Of >>>> course, this depends on marker size and the scale at which I plot; if I >>>> "zoom in" there is less overlap.) >>>> >>>> It appears that when markers overlap, spplot() places a marker >>>> associated >>>> with the larger value after, and therefore on top of, a marker >>>> associated >>>> with a smaller value. >>>> >>>> Am I correct? Or more generally, what is the algorithm that >>>> determines the >>>> order in which markers are added? >>>> >>>> >>>> I have searched ?spplot and related help pages and haven't found an >>>> explanation (at least, not yet). >>>> >>>> I can add that I don't believe markers are placed in the order in which >>>> they appear in the SpatialPointsDataFrame. I say this because when I >>>> attempt to reproduce an spplot using base graphics plot() I have to sort >>>> from smallest to largest value to succeed. >>>> >>>> I can probably provide a small reproducible example if necessary, but >>>> I'm >>>> hoping it's not necessary. >>>> >>>> Here are my actual commands. >>>> >>>> tmps is the SpatialPointsDataFrame. >>>> tmp is coordinates(tmps) >>>> (they have the same number of rows in the same order) >>>> >>>> Note that I'm using the cuts argument to break a continuous variable >>>> ('cpm2') into bins. I have carefully matched the colors in tmps$col2 >>>> with >>>> the cbin.cols object passed to spplot(), and the break points for the >>>> bin >>>> boundaries, so I believe that everything else that could affect the >>>> final >>>> appearance, other than the order in which the markers are placed, is >>>> controlled. >>>> >>>> #1 using spplot >>>> spplot(tmps,c('cpm2'), >>>> key.space='right', >>>> legendEntries=cbin.lbls, >>>> cuts=cbin.brks, >>>> col.regions=cbin.cols, >>>> cex=0.4) >>>> >>>> #2 using base graphics >>>> plot(tmp[,1],tmp[,2], asp=1, cex=0.6, pch=16, col=tmps$col2) >>>> >>>> >>>> >>>> Thanks >>>> -Don >>>> >>> >> >> >> - > _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by Edzer Pebesma-2
I like to write a r-function to create 100 shape files in a loop. I
have three data files. Each file has 100 data fields (like sim1, sim2, sim3....sim100) with a unique ID fields (Thana_ID). First I want to create 100 data frame from these files and then merge or join each of this 100 files with a polygon files individually to create 100 ESRI shape files. I am trying to this following way. But I need do this in a loop. Any suggestion will be appreciated. Thanks Zia #-------------------------------------------------- library(sp) library(spdep) library(gstat) library(maptools) library(maps) library(rgdal) ###### setwd("H:/GWPR") gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) names(thana) names(was) names(gas) names(total # Create 100 data-frames by selecting one column from each file. sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, logGAs=gas$sim1, logTotal=total$sim1) sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, logGAs=gas$sim2, logTotal=total$sim2) . . . sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, logGAs=gas$sim100, logTotal=total$sim100) #Join each data framewith a shape file and then write #ESRIShape files. # Total number of shapefiles willbe100. sim1.thana<-readShapePoly("bd_thana_246.shp") sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", driver="ESRI Shapefile") sim2.thana<-readShapePoly("bd_thana_246.shp") sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", driver="ESRI Shapefile") . . . sim100.thana<-readShapePoly("bd_thana_246.shp") sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", layer="sim100.thana", driver="ESRI Shapefile") -- --------------------- Zia Ahmed, PhD Research Associate Department of Crop and Soil Sciences 1002 Bradfield Hall, Cornell University Ithaca, NY 14853-4203 t. 607.255.9387 f. 607.255.3207 email [hidden email] [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
Hi Zia,
There are two methods were you can work: the for statement and the tapply function. First, take a good look on these: ?"for" and ?tapply You can try something like this: for (i in 1:100) sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], logGAs=gas$[,i],logTotal=total$[,i]) sim.shp <- thana sim.shp@data <- merge(thana@data,sim,by.x="THANA_ID",by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim.shp,dsn="/GWPR/Shape_Files", layer=paste("sim_",i,".shp",sep=""),driver="ESRI Shapefile") A better way is use create a list of variables, so i will assume the name of the variable. On Mon, Mar 12, 2012 at 10:53 AM, Zia Ahmed <[hidden email]> wrote: > I like to write a r-function to create 100 shape files in a loop. I > have three data files. Each file has 100 data fields (like sim1, sim2, > sim3....sim100) with a unique ID fields (Thana_ID). First I want to > create 100 data frame from these files and then merge or join each of > this 100 files with a polygon files individually to create 100 ESRI > shape files. I am trying to this following way. But I need do this in a > loop. Any suggestion will be appreciated. > > Thanks > Zia > > #-------------------------------------------------- > > library(sp) > library(spdep) > library(gstat) > library(maptools) > library(maps) > library(rgdal) > > ###### > setwd("H:/GWPR") > > gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) > was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) > total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) > names(thana) > names(was) > names(gas) > names(total > > # Create 100 data-frames by selecting one column from each file. > > sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, logGAs=gas$sim1, > logTotal=total$sim1) > > sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, logGAs=gas$sim2, > logTotal=total$sim2) > . > . > . > sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, > logGAs=gas$sim100, logTotal=total$sim100) > > > > #Join each data framewith a shape file and then write #ESRIShape files. > # Total number of shapefiles willbe100. > > sim1.thana<-readShapePoly("bd_thana_246.shp") > > sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", > driver="ESRI Shapefile") > > sim2.thana<-readShapePoly("bd_thana_246.shp") > > sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", > driver="ESRI Shapefile") > > . > > . > > . > > sim100.thana<-readShapePoly("bd_thana_246.shp") > > sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", layer="sim100.thana", > driver="ESRI Shapefile") > > > > -- > --------------------- > Zia Ahmed, PhD > Research Associate > Department of Crop and Soil Sciences > 1002 Bradfield Hall, Cornell University > Ithaca, NY 14853-4203 > t. 607.255.9387 > f. 607.255.3207 > email [hidden email] > > > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Atenciosamente, Raphael Saldanha [hidden email] [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by zia207
For constructing sim1:n you could probably use mapply. The second part of
inserting the data into your read-in shapefiles could possibly be done using sapply. HTH, Roman On Mon, Mar 12, 2012 at 2:53 PM, Zia Ahmed <[hidden email]> wrote: > I like to write a r-function to create 100 shape files in a loop. I > have three data files. Each file has 100 data fields (like sim1, sim2, > sim3....sim100) with a unique ID fields (Thana_ID). First I want to > create 100 data frame from these files and then merge or join each of > this 100 files with a polygon files individually to create 100 ESRI > shape files. I am trying to this following way. But I need do this in a > loop. Any suggestion will be appreciated. > > Thanks > Zia > > #-------------------------------------------------- > > library(sp) > library(spdep) > library(gstat) > library(maptools) > library(maps) > library(rgdal) > > ###### > setwd("H:/GWPR") > > gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) > was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) > total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) > names(thana) > names(was) > names(gas) > names(total > > # Create 100 data-frames by selecting one column from each file. > > sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, logGAs=gas$sim1, > logTotal=total$sim1) > > sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, logGAs=gas$sim2, > logTotal=total$sim2) > . > . > . > sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, > logGAs=gas$sim100, logTotal=total$sim100) > > > > #Join each data framewith a shape file and then write #ESRIShape files. > # Total number of shapefiles willbe100. > > sim1.thana<-readShapePoly("bd_thana_246.shp") > > sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", > driver="ESRI Shapefile") > > sim2.thana<-readShapePoly("bd_thana_246.shp") > > sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", > driver="ESRI Shapefile") > > . > > . > > . > > sim100.thana<-readShapePoly("bd_thana_246.shp") > > sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", layer="sim100.thana", > driver="ESRI Shapefile") > > > > -- > --------------------- > Zia Ahmed, PhD > Research Associate > Department of Crop and Soil Sciences > 1002 Bradfield Hall, Cornell University > Ithaca, NY 14853-4203 > t. 607.255.9387 > f. 607.255.3207 > email [hidden email] > > > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- In God we trust, all others bring data. [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by Raphael Saldanha
Ops! Take off the $ on this line
sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], logGAs=gas$[,i],logTotal=total$[,i]) This mus be: sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was[,i], logGAs=gas[,i],logTotal=total[,i]) On Mon, Mar 12, 2012 at 11:13 AM, Raphael Saldanha < [hidden email]> wrote: > Hi Zia, > > There are two methods were you can work: the for statement and the tapply > function. > > First, take a good look on these: ?"for" and ?tapply > > You can try something like this: > > for (i in 1:100) > sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], > logGAs=gas$[,i],logTotal=total$[,i]) > sim.shp <- thana > sim.shp@data <- merge(thana@data,sim,by.x="THANA_ID",by.y="THANA_ID", > all.x=T, sort=F) > writeOGR(sim.shp,dsn="/GWPR/Shape_Files", > layer=paste("sim_",i,".shp",sep=""),driver="ESRI Shapefile") > > A better way is use create a list of variables, so i will assume the name > of the variable. > > > On Mon, Mar 12, 2012 at 10:53 AM, Zia Ahmed <[hidden email]> wrote: > >> I like to write a r-function to create 100 shape files in a loop. I >> have three data files. Each file has 100 data fields (like sim1, sim2, >> sim3....sim100) with a unique ID fields (Thana_ID). First I want to >> create 100 data frame from these files and then merge or join each of >> this 100 files with a polygon files individually to create 100 ESRI >> shape files. I am trying to this following way. But I need do this in a >> loop. Any suggestion will be appreciated. >> >> Thanks >> Zia >> >> #-------------------------------------------------- >> >> library(sp) >> library(spdep) >> library(gstat) >> library(maptools) >> library(maps) >> library(rgdal) >> >> ###### >> setwd("H:/GWPR") >> >> gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) >> was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) >> total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) >> names(thana) >> names(was) >> names(gas) >> names(total >> >> # Create 100 data-frames by selecting one column from each file. >> >> sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, logGAs=gas$sim1, >> logTotal=total$sim1) >> >> sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, logGAs=gas$sim2, >> logTotal=total$sim2) >> . >> . >> . >> sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, >> logGAs=gas$sim100, logTotal=total$sim100) >> >> >> >> #Join each data framewith a shape file and then write #ESRIShape files. >> # Total number of shapefiles willbe100. >> >> sim1.thana<-readShapePoly("bd_thana_246.shp") >> >> sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", >> by.y="THANA_ID", all.x=T, sort=F) >> >> writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", >> driver="ESRI Shapefile") >> >> sim2.thana<-readShapePoly("bd_thana_246.shp") >> >> sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", >> by.y="THANA_ID", all.x=T, sort=F) >> >> writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", >> driver="ESRI Shapefile") >> >> . >> >> . >> >> . >> >> sim100.thana<-readShapePoly("bd_thana_246.shp") >> >> sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", >> by.y="THANA_ID", all.x=T, sort=F) >> >> writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", layer="sim100.thana", >> driver="ESRI Shapefile") >> >> >> >> -- >> --------------------- >> Zia Ahmed, PhD >> Research Associate >> Department of Crop and Soil Sciences >> 1002 Bradfield Hall, Cornell University >> Ithaca, NY 14853-4203 >> t. 607.255.9387 >> f. 607.255.3207 >> email [hidden email] >> >> >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> R-sig-Geo mailing list >> [hidden email] >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >> > > > > -- > Atenciosamente, > > Raphael Saldanha > [hidden email] > -- Atenciosamente, Raphael Saldanha [hidden email] [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
I have tried your code, but it create only one shape file (sim100.shp).
Thanks Zia for (i in 1:100) sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was[,i], logGAs=gas[,i],logTotal=total[,i]) sim.shp <- thana sim.shp@data <- merge(thana@data,sim,by.x="THANA_ID",by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim.shp,dsn="/GWPR/Shape_Files", layer=paste("sim_",i,".shp",sep=""),driver="ESRI Shapefile") On 3/12/2012 10:20 AM, Raphael Saldanha wrote: > Ops! Take off the $ on this line > sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], > logGAs=gas$[,i],logTotal=total$[,i]) > > This mus be: > sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was[,i], > logGAs=gas[,i],logTotal=total[,i]) > > > On Mon, Mar 12, 2012 at 11:13 AM, Raphael Saldanha > <[hidden email] <mailto:[hidden email]>> wrote: > > Hi Zia, > > There are two methods were you can work: the for statement and the > tapply function. > > First, take a good look on these: ?"for" and ?tapply > > You can try something like this: > > for (i in 1:100) > sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], > logGAs=gas$[,i],logTotal=total$[,i]) > sim.shp <- thana > sim.shp@data <- > merge(thana@data,sim,by.x="THANA_ID",by.y="THANA_ID", all.x=T, sort=F) > writeOGR(sim.shp,dsn="/GWPR/Shape_Files", > layer=paste("sim_",i,".shp",sep=""),driver="ESRI Shapefile") > > A better way is use create a list of variables, so i will assume > the name of the variable. > > > On Mon, Mar 12, 2012 at 10:53 AM, Zia Ahmed <[hidden email] > <mailto:[hidden email]>> wrote: > > I like to write a r-function to create 100 shape files in a > loop. I > have three data files. Each file has 100 data fields (like > sim1, sim2, > sim3....sim100) with a unique ID fields (Thana_ID). First I > want to > create 100 data frame from these files and then merge or join > each of > this 100 files with a polygon files individually to create 100 > ESRI > shape files. I am trying to this following way. But I need do > this in a > loop. Any suggestion will be appreciated. > > Thanks > Zia > > #-------------------------------------------------- > > library(sp) > library(spdep) > library(gstat) > library(maptools) > library(maps) > library(rgdal) > > ###### > setwd("H:/GWPR") > > gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) > was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) > total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) > names(thana) > names(was) > names(gas) > names(total > > # Create 100 data-frames by selecting one column from each file. > > sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, > logGAs=gas$sim1, > logTotal=total$sim1) > > sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, > logGAs=gas$sim2, > logTotal=total$sim2) > . > . > . > sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, > logGAs=gas$sim100, logTotal=total$sim100) > > > > #Join each data framewith a shape file and then write > #ESRIShape files. > # Total number of shapefiles willbe100. > > sim1.thana<-readShapePoly("bd_thana_246.shp") > > sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", > driver="ESRI Shapefile") > > sim2.thana<-readShapePoly("bd_thana_246.shp") > > sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", > driver="ESRI Shapefile") > > . > > . > > . > > sim100.thana<-readShapePoly("bd_thana_246.shp") > > sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", > layer="sim100.thana", > driver="ESRI Shapefile") > > > > -- > --------------------- > Zia Ahmed, PhD > Research Associate > Department of Crop and Soil Sciences > 1002 Bradfield Hall, Cornell University > Ithaca, NY 14853-4203 > t. 607.255.9387 > f. 607.255.3207 > email [hidden email] <mailto:[hidden email]> > > > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] <mailto:[hidden email]> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > > -- > Atenciosamente, > > Raphael Saldanha > [hidden email] <mailto:[hidden email]> > > > > > -- > Atenciosamente, > > Raphael Saldanha > [hidden email] <mailto:[hidden email]> -- --------------------- Zia Ahmed, PhD Research Associate Department of Crop and Soil Sciences 1002 Bradfield Hall, Cornell University Ithaca, NY 14853-4203 t. 607.255.9387 f. 607.255.3207 email [hidden email] [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
|
In reply to this post by Raphael Saldanha
I like to write a r-function to create 100 shape files in a loop. I
have three data files. Each file has 100 data fields (like sim1, sim2, sim3....sim100) with a unique ID fields (Thana_ID). First I want to create 100 data frame from these files and then merge or join each of this 100 files with a polygon files individually to create 100 ESRI shape files. I am trying to do this with two steps. First, I have created 100 csv files. But, I when I am trying to merge these files with a shape file (Step 2), it show error massage. It create only three shape files (sim1, sim2, sim3). I have not able figured out the source of error in code. Any suggestion? Thanks Error in writeOGR(thana, dsn = "/GWPR/Shape_Files", paste("sim", i, ".shp", : Non-unique field names #------------------- library(spdep) library(gstat) library(maptools) library(maps) library(rgdal) library(foreign) ###### setwd("H:/GWPR/csv_files") id <- read.csv("id_file.csv", as.is = TRUE) gas <- read.csv('thana_sis_logGAS_intake_246.csv', as.is = TRUE) total <- read.csv("thana_sis_logTotal_intake_246.csv", as.is = TRUE) was <- read.csv("thana_sis_logWAS_intake_246.csv", as.is = TRUE) thana <- readOGR(".", "bd_thana_246") # OGR data source with driver: ESRI Shapefile Source: ".", layer: "bd_thana_246" with 246 features and 1 fields Feature type: wkbPolygon with 2 dimensions > str(thana) Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 246 obs. of 1 variable: .. ..$ THANA_ID: num [1:246] 4 20 31 37 171 46 282 305 463 67 ... ..@ polygons :List of 246 # Step: 1: CSV files for (i in 1:100){ key <- paste('sim', i, sep = "") write.csv(data.frame(THANA_ID = id$THANA_ID , PatNum = id$PatNum , LogWAS = was[[key]] , LogGAS = gas[[key]] , LogTotal = total[[key]] , stringsAsFactors = FALSE ) , paste(key, '.csv', sep = '') )} # Step: 2: Merge with for (i in 1:100){ x <- read.csv(paste('sim', i, '.csv', sep = ''), as.is = TRUE) thana@data <- merge(thana@data, x, by.x="THANA_ID",by.y="THANA_ID", all.x = TRUE, sort = FALSE) writeOGR(thana, dsn = "/GWPR/Shape_Files", paste('sim', i,".shp", sep = ''), driver="ESRI Shapefile") } -- --------------------- Zia Ahmed, PhD Research Associate Department of Crop and Soil Sciences 1002 Bradfield Hall, Cornell University Ithaca, NY 14853-4203 t. 607.255.9387 f. 607.255.3207 email [hidden email] [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [hidden email] https://stat.ethz.ch/mailman/listinfo/r-sig-geo |
| Powered by Nabble | Edit this page |
