7

我正在尝试与许多学校创建一个简单的 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
4

2 回答 2

6

根据上面 hrbrmstr 的回答,这里有一段完整的代码,它产生的符号与您的示例 SVG 非常相似。它使用 Symbola 字体,其中包含许多面向地图的符号。从这里安装它:http: //zhm.github.io/symbola/

重要的是,您需要安装字体的 TTF 版本,而不是 OTF 版本,除非您下载了额外字体包的 devtools 版本。此页面的未来探索者可能想要仔细检查额外字体不支持 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

带有灰泥板符号的地图

我可能应该添加免责声明,这可能是针对此类问题的一种骇人听闻的通用解决方案。理论上,您可以按照 symbola 文档中的说明将 svg 符号添加到自定义字体:https ://github.com/zhm/symbola/blob/master/README.md

于 2014-04-27T21:06:36.547 回答
3

如果您按照此处的说明 - 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) 

在此处输入图像描述

这并不理想,但它非常灵活。我几乎可以肯定有一种“ grob”方式来做你想做的事,但这可能会有所帮助,直到ggplotSO 上真正令人敬畏的人之一有机会添加该解决方案。

于 2014-04-27T20:34:47.163 回答