如何在栅格地图上绘制值?
How can I plot values over a raster map?
我有一张美国栅格地图,其中包含我导入的特定范围的值
我想在其上叠加来自以下格式的 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
我该怎么做?
尝试:
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 点。
试试这个解决方案:
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")
我有一张美国栅格地图,其中包含我导入的特定范围的值
我想在其上叠加来自以下格式的 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
我该怎么做?
尝试:
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 点。
试试这个解决方案:
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")