我有这样的数据集(pts):
x <- seq(-124.25,length=115,by=0.5)
y <- seq(26.25,length=46,by=0.5)
z <- 1:5290
longlat <- expand.grid(x = x, y = y)
pts <- data.frame(longlat,z)
names(pts) <- c( "lon","lat","data")
我知道我可以通过以下方式将数据框(pts)映射到地图中:
library(sp)
library(rgdal)
library(raster)
library(maps)
coordinates(pts)=~x+y
proj4string(pts)=CRS("+init=epsg:4326") # set it to long, lat
pts = spTransform(pts,CRS(" +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
pts <- as(pts, "SpatialPixelsDataFrame")
r = raster(pts)
projection(r) = CRS(" +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
plot(r)
map("usa",add=T)
如果我还有一个因素“类”
pts$class=rep(1:5,1058)
如何添加“类”的信息,以便我可以在同一张地图上拥有 5 组单色方案?