Where are the Parking meters in San Fransisco?
To find data on the SF OpenData website you can click on one of the 10 topics provided or you can search. To find data about Parking meters in SF, search on Parking meters. The first result should be a link to the Parking meters dataset.
The SF OpenData website now has maps. You need to create an account of the website to save you plots. Here is a link to a map that I made of Where are the Parking meters?
Now we are going to use the provided API to download the data into R. To get the API Endpoint, from the spreadsheet, click on Export and copy the web link.
# install.packages("RSocrata)
library("RSocrata")
df <- read.socrata("https://data.sfgov.org/resource/yuti-t9a7.json")
head(df)
Note the the location columns have been read in as char values. These should be numeric. First we will transform them into numeric columns. We will change smart_mete to a factor, which will let us color the map with two colors. Note that sapply is used to check the classes of the columns in the dataframe.
df <- transform(df, location.longitude = as.numeric(location.longitude),
location.latitude = as.numeric(location.latitude),
smart_mete = as.factor(smart_mete))
sapply(df, class)
ms_id streetname smart_mete ms_spaceid
"character" "character" "factor" "character"
meter_type street_num cap_color jurisdicti
"character" "character" "character" "character"
post_id sfparkarea on_off_str location.latitude
"character" "character" "character" "numeric"
location.needs_recoding location.longitude osp_id ratearea
"character" "numeric" "character" "character"
street_seg activesens
"character" "character"
To simplify the dataframe we will take a subset of the columns of the original dataframe: type, latitude, and longitude. We will simplify the names of the variables/columns.
df.meters <- df[,c(3,12,14)]
names(df.meters) <- c("type","latitude","longitude")
sapply(df.meters, class)
type latitude longitude
"factor" "numeric" "numeric"
Now using the package ggmap we get a map and then plot the locations of the Parking meters on the map.
#install.packages("ggmap")
library(ggmap)
Loading required package: ggplot2
Google Maps API Terms of Service: http://developers.google.com/maps/terms.
Please cite ggmap if you use it: see citation('ggmap') for details.
m <- get_map("San Fransisco", zoom=13, maptype="toner", source="stamen")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=San+Fransisco&zoom=13&size=640x640&scale=2&maptype=terrain&sensor=false
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=San%20Fransisco&sensor=false
Map from URL : http://tile.stamen.com/toner/13/1309/3165.png
Map from URL : http://tile.stamen.com/toner/13/1310/3165.png
Map from URL : http://tile.stamen.com/toner/13/1311/3165.png
Map from URL : http://tile.stamen.com/toner/13/1309/3166.png
Map from URL : http://tile.stamen.com/toner/13/1310/3166.png
Map from URL : http://tile.stamen.com/toner/13/1311/3166.png
Map from URL : http://tile.stamen.com/toner/13/1309/3167.png
Map from URL : http://tile.stamen.com/toner/13/1310/3167.png
Map from URL : http://tile.stamen.com/toner/13/1311/3167.png
# available colors for ggplot2 http://sape.inf.usi.ch/quick-reference/ggplot2/colour
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters)) + geom_point(color="dark blue")
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude, colour=type), data = df.meters)) + geom_point()
Subsetting.
df.meters.smart <- subset(df.meters, type=="Y")
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters.smart)) + geom_point(color="turquoise2")
df.meters.not.smart <- subset(df.meters, type=="N")
ggmap(m, base_layer = ggplot(aes(x = longitude, y = latitude), data = df.meters.not.smart)) + geom_point(color="indianred2")