gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:13:05 26 4
gpt4 key购买 nike

我正在尝试与许多学校一起创建一个简单的 ggmap。我可以轻松到达学校在 map 上显示为点的点(下面的代码)。但是,我想引入学校图标的图像来代替积分。

据我所知, 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

最佳答案

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

重要的是 ,除非您下载了 extrafonts 包的 devtools 版本,否则您需要安装字体的 TTF 版本,而不是 OTF 版本。此页面的 future 浏览器可能需要仔细检查 extrafonts 中是否不支持 OTF,因为我相信 OTF 是 future 。

这是 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

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

关于r - 在ggmap中使用图像作为点图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23325565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com