我正在使用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º)")
答案 0 :(得分: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)
答案 1 :(得分:0)
以下是使用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
可以使用强化的地理配准图像。