首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ggmap中将图像用作点图标

在ggmap中将图像用作点图标
EN

Stack Overflow用户
提问于 2014-04-27 23:58:21
回答 2查看 1.7K关注 0票数 7

我正在尝试创建一个包含多个学校的简单ggmap。我可以很容易地将学校显示为地图上的点(代码如下)。但是,我想引入一个学校图标的图像来代替分数。

据我所知,annotation_custom不会工作,因为它需要笛卡尔坐标。插入应该是有效的,但这将为一所学校带来图像,而不是所有学校。同样,尝试将点字符更改为图像,而不仅仅是添加图像。

我怀疑答案在于grImport、subplot,或许还有一个与geom_point对话的函数。但是,我现在很困惑。

这是可以很好地用作图标的图像类型:wikimedia graduation hat

这个问题的答案images for tick marks in ggplot2在添加图像方面做得很好,但是,我想使用图像作为点字符,并能够基于属性更改颜色、大小等。

代码语言:javascript
复制
# 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
EN

回答 2

Stack Overflow用户

发布于 2014-04-28 05:06:37

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

重要的是,除非您下载extrafonts包的devtools版本,否则您将希望安装字体的TTF版本,而不是OTF版本。这个页面的未来探索者可能想要仔细检查OTF是否在extrafonts中不受支持,因为我相信OTF是未来的。

以下是hrbrmstr描述的完整工作版本:

代码语言:javascript
复制
# 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

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

票数 6
EN

Stack Overflow用户

发布于 2014-04-28 04:34:47

如果你按照这里的说明- https://github.com/wch/extrafont -来导入字体,你可以试着找到一个好的符号字体,有一个学校(并且编码正确)。然后你可以这样做:

代码语言:javascript
复制
# 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“方法可以做你想做的事情,但这可能会有所帮助,直到某个真正优秀的ggplot开发者有机会添加这个解决方案。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23325565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档