Navigation
Main | The Epic Search for the Perfect R Text Editor »
Tuesday
Jun192012

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:

  1. You find the material useful
  2. You use it/modify for a class of your own (and please share the results)
  3. You have any major suggestions for improvements (I'm aware of most of the minor issues)

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (28)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: buy term papers
    Graphs and data visualizations are exceptionally constructive instruments for presenting big sums of material in a better gratifying plus digestible frame than a conventional spreadsheet. Bit numerous software calendars are obtainable that can imagine graphs or material visualizations, most are exceedingly sumptuous.
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: search engine
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: tesco.com
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: Zouheir
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: more info here
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: more info here
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: Ann-Marie
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: acai berry scam
    - Frank Davenport's Research Blog - Notes from A Recent Spatial R Class I Gave
  • Response
    Response: essay writing
    And to discover facts about universe is not everyone’s cup of tea, an ignorant person can’t do this, but a person with a lot of knowledge is only able to discover facts about universe and is able to analyze them.
  • Response
    Response: AllLearning
    All of the pneumatic hand tools for the homeowner is vast and growing.
  • Response
    Response: Ronit
    We offer you several tools that will help you better [url="http://www.reverso.net"]services[/url] and complete the corrections made to your texts.
  • Response
    Download MyriadPro
  • Response
    Response: dining lights
  • Response
    Thank you very much for the information you have to say this, Greetings and hopefully always successful

Reader Comments (20)

Hey, A fantastic work. Congratulations!! It's just what I've needed for my job. Thanks.

Jose

June 20, 2012 | Unregistered Commenterjose luis

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

June 21, 2012 | Unregistered CommenterDimitris K

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?

June 21, 2012 | Unregistered CommenterCatherine

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.

June 21, 2012 | Unregistered CommenterRoger Bivand

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!

June 21, 2012 | Unregistered CommenterDrBaz

@ 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.

June 21, 2012 | Registered CommenterFrank Davenport

Great job! I'm glad that you found knitr helpful :)

June 22, 2012 | Unregistered CommenterYihui

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

June 24, 2012 | Unregistered CommenterUli

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...

June 27, 2012 | Unregistered Commenterriccardo

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?

August 4, 2012 | Unregistered CommenterMatt

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!!!!

August 4, 2012 | Unregistered CommenterMatt

This lesson has really helped me. Many thanks

January 3, 2013 | Unregistered CommenterMichael

Thanks, excellent paper! It really helped me

March 7, 2013 | Unregistered CommenterGerard

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/

April 19, 2015 | Unregistered Commenterjkrolluikenshutters

Thanks, jkrolluikenshutters, your response really helped me hahaha

May 9, 2016 | Unregistered CommenterFrank N. Stein

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

February 8, 2017 | Unregistered CommenterMarlyWatford

Hi, Are the files still available? The link to sropbox doesn't seem to work anymore...

Thanks!

April 6, 2017 | Unregistered CommenterFranklin

Hi, Are the files still available? The link to sropbox doesn't seem to work anymore...

Thanks!

April 6, 2017 | Unregistered CommenterFranklin

It seems to me that it is pretty interesting to get the inforamtion like that. I am pretty good at it.

March 20, 2018 | Unregistered Commenterlanguage practice tips

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>