我正在尝试与许多学校创建一个简单的 ggmap。我可以很容易地到达学校在地图上显示为点的点(代码如下)。但是,我想引入一个学校图标的图像来代替积分。
据我所知, annotation_custom 不起作用,因为它需要笛卡尔坐标。插图应该可以,但这会为一所学校带来形象,而不是所有学校。同样,尝试将点字符更改为图像,而不仅仅是添加图像。
我怀疑答案在于 grImport、subplot 以及可能与 geom_point 对话的函数。但是,我不知所措。
这是可以很好地用作图标的图像类型:维基媒体毕业帽
这个问题的答案ggplot2 中刻度线的图像在添加图像方面做得很好,但是,我想将图像用作点字符并能够根据属性更改颜色、大小等。
# 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