如何在栅格地图上绘制值?

时间:2017-08-24 14:00:11

标签: r ggplot2 raster shapefile

我有一张美国栅格地图,其中包含我导入的特定值范围

Annual Precip

我希望以这种格式叠加来自CSV文件的点:dput(droplevels(head(points,10))):

    points <- structure(list(lat = c(37.423333, 37.423333, 35.896667, 32.834722, 
32.834722, 32.834722, 32.834722, 32.834722, 32.834722, 32.834722
), lon = c(-122.188333, -122.188333, -121.087222, -116.622222, 
-116.622222, -116.622222, -116.622222, -116.622222, -116.622222, 
-116.622222)), .Names = c("lat", "lon"), row.names = c(NA, 10L
), class = "data.frame")

我尝试fortify(prcp),但这导致了这个错误:

Error: ggplot2 doesn't know how to deal with data of class RasterLayer

我该怎么办?

TRY:

library(raster)
library(sp)

coordinates(points) = ~lon+lat
proj4string(points) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")


r <- getData("worldclim",var="bio",res=10)
r<-r[[12]] #Annual Precipitation
names(r) <- c("Prec")
r <- crop(r, extent(-130, -60, 20, 60))
spplot(r) + layer(panel.points(x, y, col="green", cex=0.1, pch=1), data=points)

但即使点数应该只有9000点,输出也是绿色的。

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个解决方案:

library(raster)
library(sp)
library(latticeExtra)

points <- structure(list(lat = c(37.423333, 37.423333, 35.896667, 32.834722, 
32.834722, 32.834722, 32.834722, 32.834722, 32.834722, 32.834722
), lon = c(-122.188333, -122.188333, -121.087222, -116.622222, 
-116.622222, -116.622222, -116.622222, -116.622222, -116.622222, 
-116.622222)), .Names = c("lat", "lon"), row.names = c(NA, 10L
), class = "data.frame")

coordinates(points) = ~lon+lat
proj4string(points) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

r <- getData("worldclim",var="bio",res=10)
r<-r[[12]] #Annual Precipitation
names(r) <- c("Prec")
r <- crop(r, extent(-130, -60, 20, 60))

spplot(r)+spplot(points, col.regions="green")

enter image description here