2017-08-24 76 views
0

我有一个美国的栅格地图具有一定范围的值,我进口如何绘制栅格地图上的值?

Annual Precip

我要覆盖的是来自一个CSV文件格式就可以了两点:dput(droplevels(头(点, 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

怎么样[这](https://gis.stackexchange.com/questions/175108/add-a-point- on-a-spplot-in-r)解决方案? – Lyngbakr

+0

@Lyngbakr我尝试过,但我得到所有的绿色,而不是点。我究竟做错了什么? – maximusdooku

回答

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