Notes from A Recent Spatial R Class I Gave
Below is a link to a pdf (compiled with the amazing knitr package) and some accompanying data for a recent short course I gave on basic spatial data import/analysis/visualization in R. The class was only two hours and some of the participants were being exposed to R for the first time so the material is limited. The class was a follow up to a previous one I did on ArcGIS. The idea was to show how to perform the same functions in R and ArcGIS and then let users decide which worked best for them (I use R for about 90% of my spatial analysis and data handling but find ArcGIS (or some GUI based GIS) pretty essential for that last 10%).
The content/main points of the course:
- Basic intro to R
- Reading in a Shapefile
- Doing a table join with a shapefile and a data.frame
- Generating random points
- Doing a point in polygon spatial join
- Reading and cropping raster data
- Doing a pixel in polygon spatial join (using extract() from the raster pacakge)
- Plotting and annotating a shapefile in ggplot2
- Making panel maps in ggplot2
The notes contain all the R-code and a lot of grammatical and spelling errors. I hope to improve and update this class over time. Please let me know if:
- You find the material useful
- You use it/modify for a class of your own (and please share the results)
- You have any major suggestions for improvements (I'm aware of most of the minor issues)
Reader Comments (20)
Hey, A fantastic work. Congratulations!! It's just what I've needed for my job. Thanks.
Jose
Great post. Thanks Frank.
Is there any case we could have a look at the code for the production of the pdf (the .rnw file).
@Dimitris: Sorry for the delayed response, your comment got stuck in my spam filter. Here is a link to the .rnw file: https://dl.dropbox.com/u/9577903/broomspatial.Rnw
Excellent guide - just what I've been looking for so thank you! The guide mentions another class you ran on ArcGIS - are the notes available anywhere?
Nice. A couple of points:
You don't need gpclib at all, avoid it as far as possible, its license is badly encumbered. Do not set gpclibPermit() unless you need to. If you do need polygon topology operations, use rgeos, by itself or through through maptools. The rgeos package is available for OSX too from CRAN Extras (like rgdal) and from CRAN.
Do not access the data slot in a Spatial*DataFrame with @, do use access functions. Your lower chunk on p. 8 could be as(ds, "data.frame") to retrieve, and more importantly ds$new <- 1:nrow(ds) (that is, Spatial*DataFrame objects do behave like data.frame objects, including modelling settings like lm()).
Never assign to the data slot of a Spatial*DataFrame directly (p. 12), do use sp::cbind() or maptools::spCbind() methods which check the row.names of the data.frame to be added with the ID key of the geometries. Assigning directly steps around sanity and ID checks which really ought to be done.
You can do the random points as you choose, but here the spsample() methods in sp get you there much more conveniently.
What a superb guide - as an R neophyte and as someone with a passion for maps, this guide (and your blog in general) is providing me with the impetus I needed to get on with something I'd been dragging my heels over after being unable to do it in another visualisation tool.
Thank you!
@ Catherine: The ArcGIS materials are available at the link below, though I think if you search around online or the ESRI website you will find more comprehensive stuff: https://dl.dropbox.com/u/9577903/broom.zip
@ Roger: Thanks for the feedback! Always great to have information directly from the source. I have still been using gpclib() because whenever I load a dependent package I still get the message that rgeos is not available, but perhaps this is because I'm on a MAC. Thanks also for the insight regarding spcbind(), I've gotten used to accessing the slots out of laziness but spcbind() is obviously more error proof. I will make updates next time I give the class.
Great job! I'm glad that you found knitr helpful :)
Hello Frank, Hello Ubuntu users,
thank you for this guide. I just wanted to have look at it and run
install.packages("gdal") on my Ubuntu 12.4 with R 2.15.0 on it.
I got the following error messages:
"..../configure: line 1353: gdal-config: command not found
no
Error: gdal-config not found
The gdal-config script distributed with GDAL could not be found.
If you have not installed the GDAL libraries, you can
download the source from http://www.gdal.org/
If you have installed the GDAL libraries, then make sure that
gdal-config is in your path. Try typing gdal-config at a
shell prompt and see if it runs. If not, use:
--configure-args='--with-gdal-config=/usr/local/bin/gdal-config'
with appropriate values for your installation.
ERROR: configuration failed for package ‘rgdal’..."
and later, after intalling the ncessary ubuntu package (see below)
"...configure: error: proj_api.h not found in standard or given locations.
ERROR: configuration failed for package ‘rgdal’..."
I couldn't install "gdal" befor I run the following commands in the ubuntu terminal:
sudo apt-get install libgdal1-dev
sudo apt-get install proj
I got the hints form these two discussion forums:
https://stat.ethz.ch/pipermail/r-help/2011-May/277327.html
http://r-sig-geo.2731867.n2.nabble.com/install-rgdal-error-on-Ubuntu-10-04-lucid-td5151789.html" title="http://r-sig-geo.2731867.n2.nabble.com/install-rgdal-error-on-Ubuntu-10-04-lucid-td5151789.html
Best regards from Vienna, Uli
Dear Frank,
thanks for this great document. I also experienced knitr a few days ago as a great tool. so for the commentators above a little minimal example: http://www.geo-affine.org/2012/06/26/reproducible-documentsanalytics-in-r-the-knitr-package/
But one question remains: why do you chose R vs. ArcGIS and not QGIS vs. ArcGIS? R is not produced for creating maps and get spatial insights...
Awesome guide! Thanks so much.
I am trying it out on my own data and running into an error I can't figure out (disclaimer: I'm pretty new to R).
I'm on section 8 trying to create the map with ggplot2 and am getting this error:
Error in unit(x, default.units) : 'x' and 'units' must have length > 0
Ideas? Help?
Also probably relevant is that I'm getting an error in the step before:
> pds$OBJECTID <- as.integer(pds$OBJECTID)
Error in `$<-.data.frame`(`*tmp*`, "OBJECTID", value = integer(0)) :
replacement has 0 rows, data has 16249
here is the r file for everything I'm trying: https://dl.dropbox.com/u/28231177/Should%20Work.R
this is the shapefile: https://dl.dropbox.com/u/28231177/sfzipcodes.zip
and this is the data: https://dl.dropbox.com/u/28231177/listens.csv
thanks!!!!
This lesson has really helped me. Many thanks
Thanks, excellent paper! It really helped me
Watch teen porno movies on. Check out these free porn videos of teenage girls who fuck hard in their first xxx audition. www.plainsite.org/dockets/1ivdm53m/district-of.../usa-v-buccafurno/
Thanks, jkrolluikenshutters, your response really helped me hahaha
You are a real professional to have written all of this information for those who need it badly.
OUR GUARANTEES:
Privacy policy
High quality of the written paper
http://dissertationwriting.services/
Experienced academic writers
We meet the deadline
Absolute confidentiality of your personal data
Hi, Are the files still available? The link to sropbox doesn't seem to work anymore...
Thanks!
Hi, Are the files still available? The link to sropbox doesn't seem to work anymore...
Thanks!
It seems to me that it is pretty interesting to get the inforamtion like that. I am pretty good at it.