Version 1.0

Spatial Data Activity

Part I--How to place data points on a map

Try to predict what the output of each R command will be and write a description. Then enter the R command and revise your description (if necessary) based on the output.

R code / library(maps)
Description
R code / map('world')
Description
R code / map('world','Mexico')
Description
R code / map('world','Canada')
Description
R code / map('state')
Description
R code / map('state','california')
Description
R code / map('county','california')
Description
R code / map('county','california,los angeles')
Description / Map of LA County (Don’t close the graphics window yet, otherwise you will have to run
this command again before you lay the points on top)

**In order to place points on this map, we need to find the longitude and latitude of the points.

These are two numbers that represent angles (in degrees) from the center of the earth to a point on its surface. Latitudes are angles from north to south –in this case we will assign the north pole a value of 180 degrees, the equator is at 0 degrees and the southpole is at -180 degrees. Longitudes are angles from east to west with 0 occurring at the Prime Meridian, a line running from the north to south poles and crossing Greenwich, England. (Greenwich is also used in defining Greenwich Mean Time or GMT.) A great description of longitude and latitude can be found at the following URL.

To translate an address to longitude/latitude—

  • Go to
  • Type in an address (For example, UCLA is: 405 Hilgard Ave 90095).
  • The numbers you want are under the map inside the parenthesis next to WKT: POINT(-118.443244 34.070666)
  • Place those numbers into a points command adding a comma (,) in between i.e. points(-118.443244 , 34.070666,pch=19,col="blue")

R code / points(-118.443244,34.070666,pch=19,col="blue")
Description / Placing a blue circle for UCLA on our current map of LA County. Keep the graphics window
open. (If you closed the graphics window, you might get an error. You will have to map LA
County again. Also, don’t forget the comma between the numbers)
Output /

Now let’s add The Staples Center:

1111 South Figueroa StreetLos Angeles, CA

R code / points(-118.265022,34.0437788,pch=19,col="red")
Description / Placing a red circle for The Staples Center on our current map of LA County. Keep the graphics window open. (If you closed the graphics window, you might get an error. You will have to map LA County again. Also, don’t forget the comma between the numbers)
Output /

1. What’s the latitude/longitude of your school?

2. Add it to the Map. Paste the new map in a document.

**How to map a zip code

R code / library(maptools)
Description / Load maptools. Ignore the weird message that it prints out.
Output / Loading required package: foreign
Loading required package: sp
Loading required package: lattice
Note: polygon geometry computations in maptools
depend on the package gpclib, which has a
restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
Checking rgeos availability as gpclib substitute:
FALSE

Your teacher will let you know where the folder zt_06_d00_shp (the zip code file) is located so that you can tell R where it is. (If it’s on the Desktop, just choose Desktop).

Windows:

File -> Change dir…

Directions: Enter the given R commands, look at the output, and answer the questions.

R code / zip <- readShapePoly("zt06_d00_shp/zt06_d00")
Description / Load California zip code information
Output
R code / plot(zip)
Description / All zip codes in California!
Output /
R code / plot(zip[zip$NAME=="90015",])
Description / Downtown Los Angeles (90015)
Output /
R code / points(-118.265022,34.0437788,pch=19,col="red")
points(-118.268681,34.040463,pch=19,col="green")
Description / Add points for The Staples Center (red) and the LA Convention Center (green)
Output /
R code / title("Downtown LA")
Description / Add a title.
Output /

3. Plot your school’s zip code. Add points for your school and 2 other neighborhood landmarks. They can be your favorite local restaurant, your house or something else. Make sure to add a title. Paste the resulting map in a document.

Part II—How to map points from a table

** Walking (and biking) in L.A.
The data we will consider first in this lesson were collected inSeptember of 2009 by the Los Angeles County Bicycle Coalition (LACBC, a non-profit organization that works to "make theentire L.A. region a safe and enjoyable place to ride." For two daysin late September, the LACBC recruited volunteers to count the numberof bicyclists and pedestrians that pass 56 different intersectionswithin Los Angeles County.
LACBC volunteers (clipboards in hand) surveyed each location in themorning (7:00-9:30am) and evening (4:00-6:30pm) on Tuesday September22 and Wednesday September 23 of 2009. Data were also collected onSaturday the 26th, but we won't consider them here. They have produceda report summarizing their findings.

Directions: Enter the R commands, note the output, answer the questions, and follow any additional directions that your teacher provides.

R code / labike <- read.csv(
url("
Description / Load bike data
Output
R code / dim(labike)
Description / How many rows of data do we have?
Output / [1] 38 6
R code / names(labike)
Description / Look, there’s latitude and longitude information!
Output / [1] "name" "longitude" "latitude" "type"
[5] "bike_count_pm" "ped_count_pm"
R code / labike[order(labike$bike_count_pm),-c(2,3)]
Description / A printout of the bike and pedestrian counts.
Output /

Back to maps

R code / map('county','california,los angeles')
Description / LA County again. (Don’t close the graphics window yet, otherwise you will have to run
this command again before you lay the points on top)
Output /
R code / points(labike$long,labike$lat)
Description / Placing the points on our current map of LA county. (If you got an error, it may be
because you closed your window.)
Output / 4. Paste in the resulting map.

5. What is an advantage to plotting the data on a map instead of just looking at the latitude/longitude numbers?

6. What would happen to the map if you had less data in the file? More data in the file? How would that affect your interpretation of the map?

7. Every time you upload a photo using the Stress/Chill App on the phone, it also uploads the latitude/longitude of your location. How could you use what you learned about plotting points on a map with your data from the phone?

Exploring Computer Science—Unit 6: Participatory Urban Sensing “R” SupplementPage 1