使用ggmap&;R中的雄蕊图:标记点和比例

使用ggmap&;R中的雄蕊图:标记点和比例,r,ggmap,stamen-maps,R,Ggmap,Stamen Maps,我正试图用ggmap和雄蕊图为我的学习地点绘制一张地图。我已经看到了一些类似的问题,但还没有找到一种方法将解决方案合并到我的雄蕊图代码中 关于这一点,我有两个问题: 1.如何自定义地图上的点标签? 2.如何在雄蕊图中为贴图添加比例?(在现实生活中,可以作为表示距离的线或类似于地图上x厘米=y公里的东西) 为了响应这个建议,我将结果粘贴到dput(head(Tcoords))这里: structure(list(trap = c("1", "2", "3", "4", "5", "6"), la

我正试图用ggmap和雄蕊图为我的学习地点绘制一张地图。我已经看到了一些类似的问题,但还没有找到一种方法将解决方案合并到我的雄蕊图代码中

关于这一点,我有两个问题: 1.如何自定义地图上的点标签? 2.如何在雄蕊图中为贴图添加比例?(在现实生活中,可以作为表示距离的线或类似于地图上x厘米=y公里的东西)

为了响应这个建议,我将结果粘贴到
dput(head(Tcoords))
这里:

 structure(list(trap = c("1", "2", "3", "4", "5", "6"), latitude = c(52.344312, 
52.342809, 52.3446849, 52.343572, 52.34431, 52.3451601), longitude = c(0.537462, 
0.538208, 0.5406787, 0.539828, 0.539705, 0.5406294)), row.names = c(NA, 
6L), class = "data.frame")
这是我用来绘制我的点的代码

center = c(lon = 0.5406294, lat = 52.3451601)
qmap(center, zoom = 16, source = "stamen", maptype = "watercolor")+ 
      geom_point(aes(x = longitude, y = latitude), size = 4, shape = 21, 
                 fill = "dark green", data = Tcoords)
但不知何故,陷阱并没有被视为一个物体。这可能是一些基本的东西,但我不确定我错过了什么(R新手)。我已将“陷阱”保存为文本对象


谢谢你的帮助

将标签放到地图上只需在
geom_text()
函数中重新定义数据源即可。
为了在地图上打印比例,需要遵循此问题的解决方案:

#获取底图

map.base我建议将
tmap
作为
ggmap
的替代方案。这是创建地图的许多其他可能的软件包之一,但我发现
tmap
生成的比例尺非常漂亮,代码也很简单

生成最终绘图的代码需要以下软件包

# To create the map
library(tmap)
# To create the layer with the points given in Tcoords.csv
library(sf)
# To read the background map
library(tmaptools)
library(OpenStreetMap)
然后,我们读取要映射的六个点的坐标,并将它们转换为sf对象

# Read coordinates
Tcoords = dget("Tcoords.R")

# create an sf object for the six points in the file
coordinates = matrix(c(Tcoords$longitude, Tcoords$latitude), 6, 2)
tcoords_sfc = lapply(1:6, function(k) st_point(coordinates[k, ])) %>%
  st_sfc(crs = 4326)
tcoords_sf = st_sf(trap = Tcoords$trap, geometry = tcoords_sfc)
接下来,我们找到六个点(边界框)的极限,并将它们扩展因子2.5。您可以使用此因子获得具有其他比例的地图

bb_new = bb(tcoords_sf, ext = 2.5)
最后我们看了背景图

map_osm = read_osm(bb_new, zoom = 15, type = "stamen-watercolor")
然后画出最后的地图

使用以下代码

tmap_mode("plot")
tm_shape(map_osm, projection = 4326, unit = "m") +
  tm_rgb() +
  tm_shape(tcoords_sf) + 
  tm_symbols(col = "darkgreen", shape = 21, size = 2) +
  tm_text(text = "trap", col = "white") +
  tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 0.6) +
  tm_compass(position = c("left", "top"))
获取动态地图更简单,因为您不需要先读取基本地图(
read_osm
),然后绘制地图

tmap_mode("view")
tm_shape(tcoords_sf, bbox = bb_new, unit = "m") + 
  tm_symbols(col = "darkgreen", shape = 21, size = 3) +
  tm_text(text = "trap", col = "white") +
  tm_basemap("Stamen.Watercolor") +
  tm_scale_bar()
在静态绘图中,可以个性化比例中的颜色、文本和打断。注意
tm_形状中的参数
unit=“m”
,以获得以米为单位的刻度,否则将以公里为单位


希望您会发现这个替代方案值得一提。

我想提供一个与
ggmap
一起使用的解决方案。Oswaldo Santos的软件包
ggsn
为使用
ggplot
ggmap
创建的地图添加了一个比例尺。它需要播放一点参数,以找到比例尺的正确位置、文本大小和文本位置

希望你觉得有用

#需要软件包
图书馆(ggmap)
#>警告:包“ggmap”是在R版本4.0.3下生成的
#>加载所需包:ggplot2
#>谷歌的服务条款:https://cloud.google.com/maps-platform/terms/.
#>请引用ggmap,如果你使用它!详见引文(“ggmap”)。
#在ggmap中绘制比例尺的包
图书馆(ggsn)
#>警告:包“ggsn”是在R版本4.0.3下生成的
#>加载所需包:网格
#坐标
tcoords=data.frame(
陷阱=c(“1”、“2”、“3”、“4”、“5”、“6”),
纬度=
c(52.344312,52.342809,52.3446849,52.343572,52.34431,52.3451601),,
经度=
c(0.537462,0.538208,0.5406787,0.539828,0.539705,0.5406294),,
(系数=F)
#获取雄蕊水彩图
中心=c(长=0.5406294,纬度=52.3451601)
bb_map=c(中心[“长”]-0.01,中心[“纬度”]-0.005,
中心[“lon”]+0.01,中心[“lat”]+0.005)
名称(BBU地图)=c(“左”、“下”、“右”、“上”)
map\u osm2=get\u stamenmap(bb\u map,zoom=15,maptype=“水彩”)
#>资料来源:http://tile.stamen.com/watercolor/15/16432/10771.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16433/10771.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16434/10771.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16432/10772.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16433/10772.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16434/10772.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16432/10773.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16433/10773.jpg
#>资料来源:http://tile.stamen.com/watercolor/15/16434/10773.jpg
#绘制地图、坐标和比例尺
ggmap(map_osm2)+
geom_点(数据=T字词,aes(x=经度,y=纬度),
大小=4,形状=21,填充=“暗绿色”)+
比例尺(
x、 min=bb_图[1],x.max=bb_图[3],
y、 min=bb_图[2],y.max=bb_图[4],
st.bottom=FALSE,dist=100,dist_unit=“m”,
transform=TRUE,model=“WGS84”,
锚=c(x=0.548,y=52.3410),
标准尺寸=3,标准距离=0.03)

由(v0.3.0)于2020年11月8日创建

map_osm = read_osm(bb_new, zoom = 15, type = "stamen-watercolor")
tmap_mode("plot")
tm_shape(map_osm, projection = 4326, unit = "m") +
  tm_rgb() +
  tm_shape(tcoords_sf) + 
  tm_symbols(col = "darkgreen", shape = 21, size = 2) +
  tm_text(text = "trap", col = "white") +
  tm_scale_bar(breaks = c(0, 50, 100, 150, 200), text.size = 0.6) +
  tm_compass(position = c("left", "top"))
tmap_mode("view")
tm_shape(tcoords_sf, bbox = bb_new, unit = "m") + 
  tm_symbols(col = "darkgreen", shape = 21, size = 3) +
  tm_text(text = "trap", col = "white") +
  tm_basemap("Stamen.Watercolor") +
  tm_scale_bar()