在ggmap中使用图像作为点图标

时间:2014-04-27 15:58:21

标签: r ggmap grimport

我正在尝试使用多个学校创建一个简单的ggmap。我可以轻松地将学校显示为地图上的点(下面的代码)。但是,我想带一个学校图标的图像来代替点。

据我所知,annotation_custom不起作用,因为它需要笛卡尔坐标。插图应该有用,但这会为一所学校带来形象,而不是全部。再次尝试将点角色更改为图像,而不仅仅是添加图像。

我怀疑答案在于grImport,subplot以及可能与geom_point对话的功能。但是,我不知所措。

这是一种可以作为图标使用的图像类型:wikimedia graduation hat

这个问题images for tick marks in 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

2 个答案:

答案 0 :(得分:6)

根据上面的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

Map with mortarboard symbol

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

答案 1 :(得分: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) 

enter image description here

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