An example R session

Set up

  • start RGui (from the desktop shortcut)
  • to copy R-code into R:
    • select the code (in a Courier font like this) by left-clicking and dragging the mouse over it
    • press ctrl-C to copy
    • press alt-Tab to switch to R
    • press ctrl-V to paste into R (at the command prompt)
    • press alt-Tab to get back here

Download and open an existing data set

  • Summit Cr. fluvial geomorph data [sumcr.csv] -- download and save to working directory

    sumcr <- read.csv("sumcr.csv")
     

  • get some summary statistics for the data set

    summary(sumcr)
     

  • plot water-surface width vs. cumulative length downstream

    attach(sumcr)
    plot(CumLen, WidthWS)
     

  • add a summarizing curve

    lines(lowess(CumLen, WidthWS), col="red")
     

  • get a matrix scatter plot of large world cities data [cities.csv]

    cities <- read.csv("cities.csv")
    plot(cities[,2:12])
     

  • Oregon climate station data [orstationc.csv]

    orclimate <- read.csv("orstationc.csv", as.is=TRUE)
    attach(orclimate)
    plot(elev,tann)
    text(elev,tann,labels=station, cex=0.7)

A simple map-drawing script

Oregon county outlines:  (orcounty [.dbf] [.sbn] [.sbx] [.shp] [.shx])

  • # simple maps of Oregon county data
    # uses shapefiles and maptools library
    library(maptools) # also load the sp package
    library(RColorBrewer)
     
  • # read a shape file with attached attribute data
    orcounty.shp <- readShapePoly(file.choose())
     
  • # turn plot history on and run graphics demos
     
  • # equal-frequency class intervals
    plotvar <- orcounty.shp@data$AREA
    nclr <- 8
    plotclr <- brewer.pal(nclr,"BuPu")
    #plotclr <- plotclr[nclr:1] # reorder colors if appropriate
    brks <- round(quantile(plotvar, probs=seq(0,1,1/(nclr))), digits=1)
    colornum <- findInterval(plotvar, brks, all.inside=T)
    colcode <- plotclr[colornum]
    table(colornum)
    brks

    plot(orcounty.shp, xlim=c(-125, -114), ylim=c(42,47))
    plot(orcounty.shp, col=colcode, add=T)
    text(-120,46.5,"Equal-Frequency Class Intervals")
    legend(-117, 44, legend=leglabs(round(brks, digits=1)),
         fill=plotclr, cex=0.6, bty="n")
     
  • # equal-width class intervals
    plotvar <- orcounty.shp@data$AREA
    nclr <- 8
    plotclr <- brewer.pal(nclr,"BuPu")
    #plotclr <- plotclr[nclr:1] # reorder colors if appropriate
    brks <- round(seq(min(plotvar),max(plotvar),
         by=((max(plotvar)-min(plotvar))/nclr), digits=1))
    colornum <- findInterval(plotvar, brks, all.inside=T)
    colcode <- plotclr[colornum]

    plot(orcounty.shp, xlim=c(-125, -114), ylim=c(42,47))
    plot(orcounty.shp, col=colcode, add=T)
    text(-120,46.5,"Equal-Width Class Intervals")
    legend(-117, 44, legend=leglabs(round(brks, digits=1)),
         fill=plotclr, cex=0.6, bty="n")
     

Some further examples with the built-in data sets

# turn plot history on and run graphics demos
windows(record=T)  # open a graphics window and records plots

demo(graphics)
demo(persp)

 

# look at the built-in data set "quakes"
summary(quakes)
attach(quakes)
plot(long, lat)

hist(depth)

hist(mag)

detach(quakes)

 

[back to topics and examples] [Geog 4/517] [Geog. 4/517 lectures]