Reputation: 1
I have spatial dataframe with sampling locations and a variaty of variables. I would like to create a spatial plot for every variable in a For loop. I used spplot for this, which is working for a single variable when I remove the loop. I have below a short description of the database and the loop I made so far. How can I construct the For loop so it produces saperate plots for every variable?
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()
}
}
Loop with making .png file
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()
}
}
Upvotes: 0
Views: 468