简体   繁体   English

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

[英]Using an image as point icon in ggmap

I am trying to create a simple ggmap with a number of schools. 我正在尝试用一些学校创建一个简单的ggmap。 I can easily get to the point where the schools show up as points on the map (code below). 我可以轻松地将学校显示为地图上的点(下面的代码)。 But, I would like to bring in an image of a school icon to use instead of the points. 但是,我想带一个学校图标的图像来代替点。

As far as I can tell, annotation_custom won't work because it needs Cartesian coordinates. 据我所知,annotation_custom不起作用,因为它需要笛卡尔坐标。 Inset should work, but this would bring in the image for one school, not all of them. 插图应该有用,但这会为一所学校带来形象,而不是全部。 Again, trying to change the point character into an image, not just add an image. 再次尝试将点角色更改为图像,而不仅仅是添加图像。

I suspect the answer lies with grImport, subplot and perhaps a function to speak with geom_point. 我怀疑答案在于grImport,subplot以及可能与geom_point对话的功能。 But, I am at a loss. 但是,我不知所措。

This is the type of image that would work well as an icon: wikimedia graduation hat 这是一种图像类型,可以很好地作为一个图标: 维基媒体毕业帽

The answer to this question images for tick marks in ggplot2 does a nice job of adding in the images, but, I would like to use the image as a point character and be able to change the color, size, etc based on attributes. 这个问题的答案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

Based on hrbrmstr's answer above, here's a complete bit of code that produces a symbol very similar to your example SVG. 根据hrbrmstr上面的答案,这里有一些完整的代码,它产生的符号与您的示例SVG非常相似。 It makes use of the Symbola font, which contains a number of map-oriented symbols. 它使用Symbola字体,其中包含许多面向地图的符号。 Install it from here: http://zhm.github.io/symbola/ 从这里安装:http: //zhm.github.io/symbola/

Importantly , you'll want to install the TTF version of the font, rather than the OTF version, unless you download the devtools version of the extrafonts package. 重要的是,除非您下载devtools版本的extrafonts软件包,否则您需要安装字体的TTF版本而不是OTF版本。 Future explorers to this page may want to double check that OTF isn't supported in extrafonts, as I am led to believe OTF is The Future. 此页面的未来探索者可能想要仔细检查外部不支持OTF,因为我被认为OTF是未来。

Here's a full working version of what hrbrmstr describes: 这是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

地图与灰泥板符号

I should probably add the disclaimer that this could be something of a hacky, general solution for this kind of problem. 我应该添加免责声明,这可能是针对此类问题的一种hacky,通用解决方案。 You could, in theory, add svg symbols to a custom font as described in the symbola documentation: https://github.com/zhm/symbola/blob/master/README.md 理论上,您可以将svg符号添加到自定义字体中,如symbola文档中所述: https//github.com/zhm/symbola/blob/master/README.md

If you follow the instruction here - https://github.com/wch/extrafont - to import fonts, you can try to find a good symbol font that has a school (and is encoded properly). 如果你按照这里的说明 - https://github.com/wch/extrafont - 导入字体,你可以尝试找到一个有学校的好符号字体(并且编码正确)。 Then you can do something like this: 然后你可以做这样的事情:

# 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) 

在此输入图像描述

It's not ideal, but it's pretty flexible. 它并不理想,但它非常灵活。 I'm almost certain there's a " grob " way to do what you want, but this may help until one of the truly awesome ggplot ers on SO have a chance to add that solution. 我几乎可以肯定,有一种“ grob ”方式可以做你想做的事情,但这可能会有所帮助,直到SO上真正令人敬畏的ggplot ers之一有机会添加该解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM