如何在 R 中的点图中使用地理参考图像?

How using georeferenced image in a plot of points in R?

我正在使用 R 的绘图函数绘制下面的图。这个情节 以不同点值的形式表示,是从南美洲巴西海岸的不同位置获得的。图中的颜色点因值向量而异。

为了在此图中更好地表示南美海岸,我想在下方插入一个显示海岸线的地理参考图像文件。我希望这些点留在海岸线上以便更好地表示。可以在 R 中执行此操作吗?

我已经有了地理参考图像,我正在使用下面的代码绘制点图。

coordinates = data[,2:3]
groups = data[,4:9]

## Scale the groups so they are all between 0 and 1
max.groups = max(groups)
min.groups = min(groups)
scaled.groups = (groups-min.groups)/(max.groups-min.groups)
# Plot with color gradient
max.saz = max(groups$G1)
min.saz = min(groups$G1)
scaled.saz = (groups$G1-min.saz)/(max.saz-min.saz)
## Colors ranging from red (very) to blue (little)
saz.colors = rgb(red=scaled.saz,green=0,blue=(1-scaled.saz))
plotG1 = plot(coordinates$long, coordinates$lat, pch = 16, cex = 2, col = saz.colors, bg = saz.colors, xlab="Longitude (Wº)", ylab="Latitude (Sº)")

这是一个通用示例:

library(raster)
bra <- getData('GADM', country='BRA', level=0)
crd <- matrix(c(-44.53, -38.61, -35.91, -37.96, -42, -35.1, -40.5, -45.83, -37.11, -43.85, -1.9, -12.74, -9.88, -12.5, -2.79, -8.69, -20.67, -1.19, -4.94, -2.48), ncol=2)

plot(bra)
points(crd, pch=20, col=topo.colors(10), cex=2)

仅获取海岸线:

x <- as(bra, 'SpatialLines')
plot(x)
y <- crop(x, drawExtent())
# draw a box on the plot by clicking in two corners
# wait for 10 secs.

现在

plot(y)
points(crd, pch=20, col=topo.colors(10), cex=2)

但是你说你有一个地理参考图像。原则上(假设它具有相同的坐标参考系)你应该能够做这样的事情:

r <- raster("image.tif")
plot(r)
points(crd, pch=20, col=topo.colors(10), cex=2)

这是一种使用 ggplot2 的方法(模仿@RobertH):

library(raster)
library(rgeos)
library(ggplot2)
library(ggthemes)
library(viridis)

# retrieve map
bra <- getData('GADM', country='BRA', level=0)

# simplify the map so it plots more quickly
bra_simpl <- gSimplify(bra, 0.01, topologyPreserve=TRUE)

# make it work with ggplot2
bra_map <- fortify(bra_simpl)

# simulate some data
dots <- data.frame(x=c(-44.53, -38.61, -35.91, -37.96, -42, -35.1,
                       -40.5, -45.83, -37.11, -43.85),
                   y=c(-1.9, -12.74, -9.88, -12.5, -2.79, -8.69, -20.67, 
                       -1.19, -4.94, -2.48),
                   value=sample(100, 10))

gg <- ggplot()
# lay down base layer
gg <- gg + geom_map(map=bra_map, data=bra_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.15, fill=NA)
# plot your points
gg <- gg + geom_point(data=dots, aes(x=x, y=y, color=value), size=4)
# better colors
gg <- gg + scale_color_viridis()
# projection & limit map viewport
gg <- gg + coord_map(xlim=c(-53,-34), ylim=c(-30,0))
# clean map plot
gg <- gg + theme_map()
# legend on right
gg <- gg + theme(legend.position="right")
gg

geom_raster 可以处理强化的地理参考图像。