Skip to content

newbie questions

14 messages · Tom Sgouros, Roger Bivand, tom sgouros +7 more

#
Hello all:

I'm trying to make some simple maps of election results and I am having
some trouble.  I'm hoping to use Grass (or QGis) to make the maps, but
apparently this requires getting my data into the shape files, and I
can't figure out how to do that.  I've found read.shape and that's
great, and I thought that by editing the resulting att.data data frame I
could do what I need.  Unfortunatly, if I use merge to combine my data
with the existing data, the data frame comes out sorted by the join
column and the output dbf file (done with write.dbf) doesn't work
properly with the shapefile because it's no longer sorted the same as
the outlines.

I tried preserving the original order of the data frame and re-sorting
the data frame before writing it out, and that worked, but the resulting
dbf file causes a seg fault in Grass when I try to read it with the rest
of the shape data.  Plus it seems a ridiculous amount of work for what
must be a fairly routine operation.

So here's my question: I have some data I want to add to a shape file
(the SHP, DBF, etc).  Can I use R maptools to do that easily, and if so,
how?

Many thanks in advance for the help,

 -tom
#
On Sun, 15 Nov 2009, Tom Sgouros wrote:

            
Please do not use read.shape(), it is deprecated, and will become 
unavailable at the user-visible level at the next maptools release.

Do use readShapeSpatial() (or better readOGR() in rgdal) to create a 
SpatialPolygonsDataFrame, paying attention to the ID= argument. Then use 
spCbind() and if necessary spChFIDs() to ensure that your data are being 
associated with the correct Polygons object inside the 
SpatialPolygonsDataFrame. Then use writeSpatialShape() (or writeOGR()) to 
write the shapefile.
Using merge() without reading the help file usually has this outcome - 
there are arguments controlling sorting.

Hope this helps,

Roger

PS. Did you read the Spatial task view on CRAN, or, say, the page on the R 
wiki entitled "Getting spatial data into and out of R":

http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data:import_export

  
    
#
Roger Bivand <Roger.Bivand at nhh.no> wrote:

            
Where is the introduction that explains all this?  Is there one
somewhere?   (And thank you for the assistance.)
Thank you very much for all that, it gives me a lot to try.
Thank you for the help, but no thank you for the snide.  What the help
file says (on my system) is that if you do sort=FALSE, the output order
is unspecified, which is approximately zero help for someone trying to
figure out why the output map looks so funny.
Yes.  I can't say I found those pages very helpful since they are mostly
written in a language I don't understand, and where I do understand what
they're saying, they're saying things like this: 

 "Use a GIS if you're trying to do the following:

    - specialized GIS tasks"

In the nicest and most constructive way I can think of, I have to say
that this isn't very helpful to the person who has data they want to map
and wants to get on with it.  I have been a R user (and fan) for years
and am more or less familiar with much of it, but I haven't used it for
spatial data, and I find the available documentation for people who are
starting from zero like me is fairly wanting.

Many thanks,

 -tom
b> > of the shape data.  Plus it seems a ridiculous amount of work for what

  
    
#
Raphael Saldanha <saldanha.plangeo at gmail.com> wrote:

            
This is exactly my quandary.  I have some data I want to map, and I have
no idea how to do it and none of the introductions I've found speak to
what I thought was a pretty simple task.

I first thought maybe I should use a GIS, since it's geographic data,
but all the GIS documentation I can find is about how to analyze data
that's already available.  There are no end of Grass examples using this
North Carolina dataset that comes with the installation, but I don't
live in North Carolina and I don't want to analyze their data.  I want
to analyze mine, but I can't figure out how to get it in, and that's
what I'm using R for.

If you are telling me there is a simpler way to do this, I am all ears
(eyes), especially if it uses grass or qgis which I can afford, and not
Arc, which I can't.

Queries of GIS guys told me that what I need to do is to get my data
into a shape file first and then a GIS can help me display it, so I
turned to R for that.  Was I wrong there, too?

Most of the data I want to map is not currently in any format at all.
It's just numbers I have.  I have shape files for the geography and I
want to apply my numbers to those shapes and draw maps in color, and I
also would very much like to draw the 3d kind where the geographic area
is raised up according to some value.  (I don't know if there's a name
for this.)  I tried to use a spreadsheet to get my data attached to the
shapes, but as soon as I realized I needed to do a join of my data and
the shape file data I began to look for alternatives.

I know that I'm working at a very low level here, but in addition to
Roger's advice before about how to get R to cooperate, I would be
grateful for any advice such as you've provided about what exactly R is
good for in my task and what a GIS is good for.  Your note advances me
down the field quite a bit, but if there's more, I'd be interested to
know it.

Many thanks,

 -tom

  
    
#
On Sun, 15 Nov 2009 19:52:48 +0100 (CET) Roger Bivand
<Roger.Bivand at nhh.no> wrote:
Why (or how) is readOGR better?
#
Raphael Saldanha wrote:
Hi,

As you already stated, making the maps in R is quite possible. I make 
all my geographic illustrations for my publications in R. The advantage 
that I don't have do something first in Excel or ArcGIS and then 
something in R and ending in yet another tool. I like the fact that my 
whole workflow is in one versatile tool, R. So for me learning to make 
maps in R payed off. In addition, you can make awesome maps, especially 
of mulitvariate spatial data, in R. In addition, I use linux (Debian) 
and the desktop GIS tools that I tried (svSig, Qgis, and some more) did 
not provide publication quality maps. ArcMap does, but this is not 
available under Linux. If you use Windows this is not a problem 
ofcourse. See the link below for an animation I made using R (and some 
other linux tools):

http://intamap.geo.uu.nl/~paul/Images/animation_25fps.mpg

All of the above is purely my opinion :).

cheers and good luck,
Paul

  
    
#
Hello all,
An alternative to create (possibly simple) shapefiles is the 
"shapefiles" package.

http://cran.r-project.org/web/packages/shapefiles/index.html.

Also the "PBSmapping" package is a very good alternative to make maps 
within R, and to perform basic GIS operations (i.e. overlap of polygons, 
identification of locations within polygons, etc.).

http://cran.r-project.org/web/packages/PBSmapping/index.html

Nevertheless, I do think that is worth while to take a bit of time and 
learn to use the classes and methods for spatial data in the "sp" 
package.  The Bivand et al. book (Applied Spatial Data Analysis with R) 
is a great place to start.
All the best,

Julian
#
I would strongly suggest you take another look at some of these
packages (especially QGIS).
These are relatively young projects, and are growing (changing)
quickly. For example, just
recently, some very advanced symbology enhancements have been made to
the QGIS project,
and will likely be available in the next release (due out next month).
Though of course, for graphs
and other statistical outputs, nothing beats R!

Carson
#
On Mon, 16 Nov 2009, Karl Ove Hufthammer wrote:

            
Because it uses OGR, with an updated rather than frozen shapelib, and 
reads the *.prj file of the shapefile bundle as well, which 
readShapeSpatial() does not. On the other hand, readShapeSpatial() has 
various extra arguments to tackle out-of-spec shapefile bundles.

With reference to other alternatives, the shapefiles package has not been 
updated for three years, and importShapefile() in PBSmapping is built 
around read.shape() from maptools to read into the classes used in that 
package.

Roger

  
    
#
On Sun, 15 Nov 2009, tom sgouros wrote:

            
OK. Once you've tried this - there are at least two pitfalls, one that the 
shapes (features) occur multiple times for each district that you have 
data for (districcts with multiple islands or parts), the other that the 
polygons/features do not match the data by ID for other reasons. This is 
never simple, never automatic, and rarely obvious.

In GIScience one uses the term "ontology" for different people's views of 
the world, in particular the data and data objects that they use. If you 
are combining boundaries (for a given date) from one source with its 
implicit "view of the world", and election data of different provenance, 
with a different "view of the world", say different IDs for the same 
units, you will always need to spend time working out how to match the 
data to the boundaries. The tools are there, but it isn't going to get 
automatic, and you'll have the same problems matching in a database or a 
GIS. The only exceptions are the trite ones, where the same organisation 
with the same view of the world is the source of the boundaries, the IDs, 
and the data to be assigned.
With merge(), you always have to try things out until they come right. 
Perhaps reshape() is harder to use, but not by much. Both are powerful, 
but without experimenting, tough.
OK, so perhaps improve the wiki?

Including a code snippet demonstrating your problem and posting a link to 
a simple data set (shape file plus election data) would be a more 
constructive way to proceed, no? The code in Ch 5 in our book on 
www.asdar.org covers a very similar case, and uses the functions and 
methods I suggested. Without your code, it is hard to be more specific 
than pointing you to the tools you need.

Roger

  
    
#
Roger Bivand wrote:
...
...

The book link Roger meant is: http://www.asdar-book.org
#
On Monday 16 November 2009 08:53:03 Carson Farmer wrote:
Let me strongly second Carson's points. As a Debian user, I like QGIS and use 
it for my own visualization purposes. Being able to drag and drop a shapefile 
is pretty awesome. I suspect QGIS will continue to improve in its ability to 
render maps. That said, I don't think publication-quality output is what these 
software are about, at least not yet. R on the other hand gives you tremendous 
control. I used it exclusively for all maps, as well as other graphics, in my 
latest article.

The same goes for non-free GIS software, by the way. My cartographer 
colleagues who use ArcGIS generally haul mapping output from there into other 
packages for font and other effect rendering.

Yours,

Ashton
#
Ashton Shortridge wrote:
Let me stress that I like qgis very much. Only at the time I tried to 
make pub. quality maps with qgis (using version 0.9-2) it could not 
export these kind pub. quality maps. Maybe now, or in the close future, 
qgis or other open source tools will support pub. quality maps. I would 
like that, being able to just take the north arrow and drag and drop it 
would be much better than specifying the spatial coordinates. So for me 
R was the best solution at the time (the only btw), but qgis might be a 
better alternative in the future. But not yet (please correct me if I'm 
wrong).

cheers,
Paul