【问题标题】:How using georeferenced image in a plot of points in R?如何在 R 中的点图中使用地理参考图像?
【发布时间】:2016-04-29 17:58:42
【问题描述】:

我正在使用 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º)")

【问题讨论】:

  • 你有什么样的地理参考文件?形状文件?
  • 不,这只是 TIF 格式的地理参考图像。

标签: r plot raster


【解决方案1】:

这是一个通用示例:

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)

【讨论】:

  • 感谢罗伯特的帮助。我成功地在巴西地图上绘制了我的点。但我打算在我的情节中只使用巴西或南美洲的海岸线。因此,我得到了一个地理参考图像,如上图。你知道我如何使用这个地理参考图像或其他方式来获取 R 中的海岸线吗?
  • 我在我的回答中添加了一个示例,说明您如何解决这个问题。
  • 嗨罗伯特,感谢您的代码即将获得海岸线。这是一种有用且简单的制作方法。在看到您的答案之前,我尝试使用地理参考图像作为您上面的建议(r
【解决方案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 可以处理强化的地理参考图像。

【讨论】:

  • 您好,感谢您提供 ggplot2 的详细示例。我对使用此功能非常感兴趣,因为它们的效果很好,传说也很好。但是,当我在上面的示例中运行此特定代码 gg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
相关资源
最近更新 更多