I am trying to create a simple ggmap with a number of schools. I can easily get to the point where the schools show up as points on the map (code below). But, I would like to bring in an image of a school icon to use instead of the points.
As far as I can tell, annotation_custom won't work because it needs Cartesian coordinates. Inset should work, but this would bring in the image for one school, not all of them. Again, trying to change the point character into an image, not just add an image.
I suspect the answer lies with grImport, subplot and perhaps a function to speak with geom_point. But, I am at a loss.
This is the type of image that would work well as an icon: wikimedia graduation hat
The answer to this question images for tick marks in ggplot2 does a nice job of adding in the images, but, I would like to use the image as a point character and be able to change the color, size, etc based on attributes.
# Load needed packages
# install.packages(c("rgdal", "rgeos", "maptools", "ggmap", "sp", "plyr", "XML", "grImport"))
library(rgdal)
library(rgeos)
library(maptools)
library(ggmap)
library(sp)
library(plyr)
library(XML)
library(grImport)
# Define a value for the Seattle Public Schools (SPS) url:
SPSurl <- "http://www.seattleschools.org/modules/cms/pages.phtml? pageid=197023&sessionid=95b8499fc128fde5d7e1335751c73fee&t"
# All of the addresses for SPS, multiple tables:
SPSaddresses <- readHTMLTable(SPSurl)
# Just elementary schools
SPSelementary <- readHTMLTable(SPSurl, which=3, header=T)
# Just keep the names of the schools and addresses
SPSelementary <- SPSelementary[,c(1,3)]
# Change the address column name
colnames(SPSelementary)[2] <- "address"
# Convert all to character
SPSelementary <-
data.frame(lapply(SPSelementary,
as.character),
stringsAsFactors=FALSE)
# get rid of the phone numbers in the address
SPSelementary$address <- substr(SPSelementary$address,
1,
nchar(SPSelementary$address)-14)
# get rid of extra space at end of line
SPSelementary$address <- sub("[[:blank:]]+$",
"",
SPSelementary$address)
# get the longitude and latitude of the school addresses
SPSelementary_lonlat <- geocode(SPSelementary$address)
# combine addresses with longitude and latitude data
SPSelementary$id <- rownames(SPSelementary)
SPSelementary_lonlat$id <- rownames(SPSelementary_lonlat)
SPSelementary_ll <- merge(SPSelementary,
SPSelementary_lonlat,
by="id")
# Get a map of the area around the McDonald school
McDonald_map <- get_map("144 NE 54th Street Seattle WA 98105",
zoom=15,
maptype='roadmap')
McDonald_map_plot <-
ggmap(McDonald_map)
McDonald_map_plot
# Add the schools
McDonald_map_plot <- McDonald_map_plot +
geom_point(data=SPSelementary_ll,
mapping=aes(x=lon,
y=lat),
shape = 17, ### This be a triangle, want to change to school.
size = 4,
alpha=.75)
McDonald_map_plot