在 R 中使用 ggmap 和 Stamen 地图进行映射:标记点和比例
Mapping using ggmap & Stamen maps in R: labelling points and scale
我正在尝试使用 ggmap 和 Stamen 地图制作我的研究地点的地图。我已经看到了一些类似的问题,但还没有找到将解决方案合并到我的 Stamen 地图代码中的方法。
对此我有两个问题:
1. 如何自定义标注地图上的点?
2.如何在Stamen map中给地图添加比例尺? (要么是一条表示距离的线,要么是地图上的 x cm = 现实生活中的 y km)
Tcoords <- read.csv("Tcoords.csv")
我的文件是这样的
# trap latitude longitude
1 52.34431 0.5374620
2 52.34281 0.5382080
3 52.34468 0.5406787
4 52.34357 0.5398280
5 52.34431 0.5397050
6 52.34516 0.5406294
为了回应建议,我已将结果粘贴到 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)
但不知何故 trap 没有被识别为一个对象。这可能是一些基本的东西,但我不太确定我错过了什么(R 的新手)。我已将 "trap" 保存为此处的文本对象。
感谢您的帮助!
将标签添加到地图上只是在 geom_text()
函数中重新定义数据源的问题。
为了让比例尺打印在地图上,需要遵循这个问题中的解决方案:Is there a way to add a scale bar (for linear distances) to ggmap?
#get base map
map.base <- get_map(location = center, zoom = 16, source = "stamen", maptype = "watercolor") # could also use zoom = "auto"
#get extent of base map
bb <- attr(map.base,"bb")
#define the location and length of scale bar
sbar <- data.frame(lon.start = c(bb$ll.lon + 0.1*(bb$ur.lon - bb$ll.lon)),
lon.end = c(bb$ll.lon + 0.25*(bb$ur.lon - bb$ll.lon)),
lat.start = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)),
lat.end = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)))
#Calculate distance in meters
library(geosphere)
sbar$distance = distGeo(c(sbar$lon.start,sbar$lat.start), c(sbar$lon.end,sbar$lat.end))
map.scale <- ggmap(map.base, extent="device") +
geom_point(aes(x = longitude, y = latitude), size = 4, shape = 21, fill = "dark green", data = Tcoords) +
geom_text(data=Tcoords, aes(label=trap, x = longitude, y = latitude), nudge_x = 0.0001, nudge_y = 0.0001, color="black") +
geom_segment(data = sbar, aes(x = lon.start, xend = lon.end, y = lat.start, yend = lat.end)) +
geom_text(data = sbar, aes(x = (lon.start + lon.end)/2,
y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat),
label = paste(format(distance, digits = 4, nsmall = 2), 'm')),
hjust = 0.5, vjust = 0)
map.scale
可能需要调整 geom_text()
函数中的 nudge_x & _y 以正确放置标签。
我想建议 tmap
作为 ggmap
的替代方案。这是许多其他可能用于创建地图的软件包之一 CRAN Task View: Spatial 但我发现 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_shape
中的参数 unit = "m"
以获得以米为单位的比例,否则将以公里为单位。
希望您会发现这个替代方案值得一提。
我想提供一个适用于 ggmap
的解决方案。 Oswaldo Santos link 的软件包 ggsn
为使用 ggplot
或 ggmap
创建的地图添加了比例尺。需要稍微调整一下参数才能找到正确的比例尺位置、文本大小和文本位置。
希望您觉得它有用。
# packages required
library(ggmap)
#> Warning: package 'ggmap' was built under R version 4.0.3
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
# package to draw scale bars in ggmap
library(ggsn)
#> Warning: package 'ggsn' was built under R version 4.0.3
#> Loading required package: grid
# coordinates
tcoords = data.frame(
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),
stringsAsFactors = F)
# get the stamen watercolor map
center = c(lon = 0.5406294, lat = 52.3451601)
bb_map = c(center["lon"] - 0.01, center["lat"] - 0.005,
center["lon"] + 0.01, center["lat"] + 0.005)
names(bb_map) = c("left", "bottom", "right", "top")
map_osm2 = get_stamenmap(bb_map, zoom = 15, maptype = "watercolor")
#> Source : http://tile.stamen.com/watercolor/15/16432/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16432/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16432/10773.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10773.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10773.jpg
# plot the map, coordinates and scalebar
ggmap(map_osm2) +
geom_point(data = tcoords, aes(x = longitude, y = latitude),
size = 4, shape = 21, fill = "darkgreen") +
scalebar(
x.min = bb_map[1], x.max = bb_map[3],
y.min = bb_map[2], y.max = bb_map[4],
st.bottom = FALSE, dist = 100, dist_unit = "m",
transform = TRUE, model = "WGS84",
anchor = c( x = 0.548, y = 52.3410),
st.size = 3, st.dist = 0.03)
由 reprex package (v0.3.0)
于 2020-11-08 创建
我正在尝试使用 ggmap 和 Stamen 地图制作我的研究地点的地图。我已经看到了一些类似的问题,但还没有找到将解决方案合并到我的 Stamen 地图代码中的方法。
对此我有两个问题: 1. 如何自定义标注地图上的点? 2.如何在Stamen map中给地图添加比例尺? (要么是一条表示距离的线,要么是地图上的 x cm = 现实生活中的 y km)
Tcoords <- read.csv("Tcoords.csv")
我的文件是这样的
# trap latitude longitude
1 52.34431 0.5374620
2 52.34281 0.5382080
3 52.34468 0.5406787
4 52.34357 0.5398280
5 52.34431 0.5397050
6 52.34516 0.5406294
为了回应建议,我已将结果粘贴到 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)
但不知何故 trap 没有被识别为一个对象。这可能是一些基本的东西,但我不太确定我错过了什么(R 的新手)。我已将 "trap" 保存为此处的文本对象。
感谢您的帮助!
将标签添加到地图上只是在 geom_text()
函数中重新定义数据源的问题。
为了让比例尺打印在地图上,需要遵循这个问题中的解决方案:Is there a way to add a scale bar (for linear distances) to ggmap?
#get base map
map.base <- get_map(location = center, zoom = 16, source = "stamen", maptype = "watercolor") # could also use zoom = "auto"
#get extent of base map
bb <- attr(map.base,"bb")
#define the location and length of scale bar
sbar <- data.frame(lon.start = c(bb$ll.lon + 0.1*(bb$ur.lon - bb$ll.lon)),
lon.end = c(bb$ll.lon + 0.25*(bb$ur.lon - bb$ll.lon)),
lat.start = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)),
lat.end = c(bb$ll.lat + 0.1*(bb$ur.lat - bb$ll.lat)))
#Calculate distance in meters
library(geosphere)
sbar$distance = distGeo(c(sbar$lon.start,sbar$lat.start), c(sbar$lon.end,sbar$lat.end))
map.scale <- ggmap(map.base, extent="device") +
geom_point(aes(x = longitude, y = latitude), size = 4, shape = 21, fill = "dark green", data = Tcoords) +
geom_text(data=Tcoords, aes(label=trap, x = longitude, y = latitude), nudge_x = 0.0001, nudge_y = 0.0001, color="black") +
geom_segment(data = sbar, aes(x = lon.start, xend = lon.end, y = lat.start, yend = lat.end)) +
geom_text(data = sbar, aes(x = (lon.start + lon.end)/2,
y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat),
label = paste(format(distance, digits = 4, nsmall = 2), 'm')),
hjust = 0.5, vjust = 0)
map.scale
可能需要调整 geom_text()
函数中的 nudge_x & _y 以正确放置标签。
我想建议 tmap
作为 ggmap
的替代方案。这是许多其他可能用于创建地图的软件包之一 CRAN Task View: Spatial 但我发现 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_shape
中的参数 unit = "m"
以获得以米为单位的比例,否则将以公里为单位。
希望您会发现这个替代方案值得一提。
我想提供一个适用于 ggmap
的解决方案。 Oswaldo Santos link 的软件包 ggsn
为使用 ggplot
或 ggmap
创建的地图添加了比例尺。需要稍微调整一下参数才能找到正确的比例尺位置、文本大小和文本位置。
希望您觉得它有用。
# packages required
library(ggmap)
#> Warning: package 'ggmap' was built under R version 4.0.3
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
# package to draw scale bars in ggmap
library(ggsn)
#> Warning: package 'ggsn' was built under R version 4.0.3
#> Loading required package: grid
# coordinates
tcoords = data.frame(
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),
stringsAsFactors = F)
# get the stamen watercolor map
center = c(lon = 0.5406294, lat = 52.3451601)
bb_map = c(center["lon"] - 0.01, center["lat"] - 0.005,
center["lon"] + 0.01, center["lat"] + 0.005)
names(bb_map) = c("left", "bottom", "right", "top")
map_osm2 = get_stamenmap(bb_map, zoom = 15, maptype = "watercolor")
#> Source : http://tile.stamen.com/watercolor/15/16432/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10771.jpg
#> Source : http://tile.stamen.com/watercolor/15/16432/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10772.jpg
#> Source : http://tile.stamen.com/watercolor/15/16432/10773.jpg
#> Source : http://tile.stamen.com/watercolor/15/16433/10773.jpg
#> Source : http://tile.stamen.com/watercolor/15/16434/10773.jpg
# plot the map, coordinates and scalebar
ggmap(map_osm2) +
geom_point(data = tcoords, aes(x = longitude, y = latitude),
size = 4, shape = 21, fill = "darkgreen") +
scalebar(
x.min = bb_map[1], x.max = bb_map[3],
y.min = bb_map[2], y.max = bb_map[4],
st.bottom = FALSE, dist = 100, dist_unit = "m",
transform = TRUE, model = "WGS84",
anchor = c( x = 0.548, y = 52.3410),
st.size = 3, st.dist = 0.03)
由 reprex package (v0.3.0)
于 2020-11-08 创建