我正在尝试用一些学校创建一个简单的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
Run Code Online (Sandbox Code Playgroud)
根据hrbrmstr上面的答案,这里有一些完整的代码,它产生一个与你的示例SVG非常相似的符号.它使用Symbola字体,其中包含许多面向地图的符号.从这里安装:http://zhm.github.io/symbola/
重要的是,除非您下载devtools版本的extrafonts软件包,否则您需要安装字体的TTF版本而不是OTF版本.此页面的未来探索者可能想要仔细检查外部不支持OTF,因为我被认为OTF是未来.
这是hrbrmstr描述的完整工作版本:
# Load needed packages
# install.packages(c("rgdal", "rgeos", "maptools", "ggmap", "sp", "plyr", "XML", "rgdal", "grImport"))
library(rgdal)
library(rgeos)
library(maptools)
library(ggmap)
library(sp)
library(plyr)
library(XML)
library(extrafont)
font_import(pattern="Symbola", prompt=FALSE)
# 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, header=T)
# Just elementary schools
SPSelementary <- readHTMLTable(SPSurl, which=3)
# 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")
SPSelementary_ll$marker <- "?"
# 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 <- McDonald_map_plot +
geom_text(data=SPSelementary_ll,
mapping=aes(x=lon,
y=lat, label=marker, family="Symbola"),
size = 16)
McDonald_map_plot
Run Code Online (Sandbox Code Playgroud)
我应该添加免责声明,这可能是针对此类问题的一种hacky,通用解决方案.理论上,您可以将svg符号添加到自定义字体中,如symbola文档中所述:https://github.com/zhm/symbola/blob/master/README.md
如果您按照此处的说明 - https://github.com/wch/extrafont - 导入字体,您可以尝试找到有学校(并且编码正确)的良好符号字体。然后你可以做这样的事情:
# add a market object that is the proper
# symbol location in the font family
SPSelementary_ll$marker <- "A"
# Use geom_text() vs geom_point()
# you may (will?) need to do some tweaks to the position of the symbol
McDonald_map_plot <- McDonald_map_plot +
geom_text(data=SPSelementary_ll,
mapping=aes(x=lon,
y=lat, label=marker, family="Wingdings-Regular", fontface="plain"),
size = 10,
alpha=.75)
Run Code Online (Sandbox Code Playgroud)
这并不理想,但非常灵活。我几乎可以肯定有一种“ grob
”方法可以做你想做的事,但这可能会有所帮助,直到ggplot
SO 上真正出色的人之一有机会添加该解决方案。