我有空间数据框,包含采样位置和变量变量。我想为For循环中的每个变量创建一个空间图。我使用了spplot,当我删除循环时,它正在处理单个变量。 我在下面简要描述了数据库和我到目前为止所做的循环。如何构造For循环,以便为每个变量生成单独的图?
class : SpatialPointsDataFrame
features : 47
extent : -96.57795, -96.56407, 39.10135, 39.10672 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4269 +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
variables : 18
names : Sample_id, x, y, bulk_density, Percentage_SOM, Clay, Silt, Very_fine_sand, fine_sand, medium_sand, stone, Composite_depth, percentage_water, percentage_roots, Percentage_SOM_20cm, ...
min values : KLTER/K20A-1, -96.56407, 39.10135, 550.7, 6.9, 3.9, 66.9, 7.4, 0.0, 0.0, 0.1, 7.3, 16.8, 0.10, 4.2315, ...
max values : KLTER/K20A-9, -96.57795, 39.10672, 1031.8, 15.1, 8.5, 85.8, 18.5, 9.0, 5.2, 13.7, 20.0, 30.2, 2.16, 11.2000, ...
df <- K20A_sp3
for(i in names(df))
{
if(is.numeric(df[3,i]))
{
spplot(df, names(zcol= i),
scales=list(draw=T),
cex = 1.4,
main="Watershed K20A",
col.regions=brewer.pal(6, "Oranges"))
dev.off()
}
}
循环制作.png文件
for(i in names(df))
{
if(is.numeric(df[3,i]))
{
dev.new()
mypath <- file.path("C:", "Users", "Ilona", "Documents", "A master thesis", "7.Rstudio", paste("Point_K20A_", i, ".png", sep=""))
png(file=mypath)
print(spplot(df, names(zcol= i),
scales=list(draw=T),
cex = 1.4,
main= paste("Watershed K20A_", i, sep = ""),
col.regions=brewer.pal(6, "Oranges")
sp.layout(K20A_DEM))))
dev.off()
}
}
答案 0 :(得分:0)
您现在可能已经找到了答案,但是从等效的question中添加了答案。
最重要的是,在循环中需要“打印”绘图:
for(i in 1:10) {
png(...)
print(spplot(...))
dev.off()
}