我想制作几种物种图。问题是当使用by
命令时,每次进行迭代时都必须创建映射。我想知道是否可以以某种方式保存地图,以便它可以自动使用。
我尝试使用recordPlot()
函数,但它给了我一个错误并生成了空白地图:
library(raster)
elevation <- getData("alt", country = "ES", mask=F)
slope <- terrain(elevation, opt = "slope",neighbors=4)
aspect <- terrain(elevation, opt = "aspect",neighbors=4)
hill <- hillShade(slope, aspect, 40, 270)
newextent<-extent(-7.99, -2.87, 39.9, 41.5)
land.pal <- colorRampPalette(c("#336600", "#F3CA89", "#D9A627",
"#A49019", "#9F7B0D", "#996600",
"#B27676", "#C2B0B0", "#E5E5E5",
"#FFFFFF"), space="Lab") (1000)
plot(disaggregate(crop(elevation,newextent), 20, method="bilinear"), col = land.pal, legend=F)
map <- recordPlot()
by(data, data$Species, FUN = function(data) {
pdf(file=paste0("map ", data$Species,".pdf"), onefile=F, width=6,height=3, pointsize=1)
plot.new()
map
points(data$Lat, data$Long, pch=21, bg="black", cex=0.85)
dev.off()
})
提前谢谢
数据:
Lat Long Species
-6.99 40.49 Sp1
-5.05 40.60 Sp1
-3.30 40.02 Sp1
-7.51 40.86 Sp1
-3.18 40.24 Sp1
-3.83 40.08 Sp1
-7.28 40.91 Sp1
-7.74 40.53 Sp1
-7.72 40.69 Sp1
-4.36 40.06 Sp1
-5.90 40.51 Sp2
-5.56 40.07 Sp2
-3.52 40.19 Sp3
-6.29 40.93 Sp3
-6.77 40.12 Sp3
-7.11 40.77 Sp4
-5.81 40.88 Sp4
-5.27 40.68 Sp4
-6.31 40.34 Sp4
-6.13 40.32 Sp4
-6.73 40.99 Sp4
-5.32 40.71 Sp4
-6.17 40.97 Sp4
-4.77 40.92 Sp4
-6.45 40.58 Sp5
-5.54 40.39 Sp5
-6.56 40.66 Sp5
-5.27 40.37 Sp5