简体   繁体   English

使用spplot在R中的for循环中绘制图

[英]Make plots in a for loop in R with spplot

I'm having a little trouble programming stuff in R that I would normally do in a for loop in another language... 我在用R语言编写程序时遇到了一些麻烦,通常我会在另一种语言的for循环中执行此操作...

I am now trying to make a plot using spplot from the sp package (it's a chloropleth map), but I want to do it for a bunch of variables. 我现在正在尝试使用sp程序包中的spplot绘制图(这是一个chloropleth映射),但是我想针对一堆变量进行绘制。 In a for loop, I would program it like this: 在for循环中,我将这样编程:

for (TRAIT in c("height","bmi","whr","body_fat","income")){
pdf("plot_of_TRAIT.pdf")
spplot(MyData, "TRAIT", col.regions = my.palette, cuts = 8, lwd = 0.5)
dev.off()
}

where each instance of TRAIT should be replaced by the variables I give to the for loop (so in the filename of the pdf, and as an argument in the spplot command). TRAIT的每个实例都应替换为我提供给for循环的变量(因此在pdf的文件名中,并作为spplot命令中的参数)。 Now, obviously this code does not work, but how would one go about doing this in R? 现在,显然此代码不起作用,但是如何在R中执行此操作呢?

UPDATE: 更新:

I got the code to create different pdf files, but the spplot command still will not work. 我得到了创建不同pdf文件的代码,但是spplot命令仍然无法使用。 It creates empty pdf files: 它会创建空的pdf文件:

for (TRAIT in c("height","bmi","whr","body_fat","income")){
outfile <- paste0("plot_of_", TRAIT, ".pdf")
pdf(outfile)
spplot(MyData, TRAIT, col.regions = my.palette, cuts = 8, lwd = 0.5)
dev.off()
}

If I run the exact same code outside of the for loop like this: 如果我在for循环之外运行完全相同的代码,如下所示:

pdf("plot_of_height.pdf")
spplot(MyData, "height", col.regions = my.palette, cuts = 8, lwd = 0.5)
dev.off()

it works fine and I get the desired plot in the file plot_of_height.pdf. 它工作正常,并且我在文件plot_of_height.pdf中获得了所需的图。

UPDATE 2: This seems to be an issue with spplot. 更新2:这似乎是spplot的问题。 If I run this: 如果我运行此命令:

TRAIT <- "height"
pdf("plot_of_height.pdf")
spplot(MyData, TRAIT, col.regions = my.palette, cuts = 8, lwd = 0.5)
dev.off()

I get the following error: 我收到以下错误:

Error in [.data.frame(obj@data, zcol) : undefined columns selected

You need to construct the output file name using the variable. 您需要使用变量构造输出文件名。

For example: 例如:

outfile <- paste0("plot_of_", TRAIT, ".pdf")
pdf(outfile)

For spplot you probably need to use TRAIT (no quotes). 对于spplot您可能需要使用TRAIT (无引号)。

EDIT: 编辑:

Since spplot(zcol = ... can be a column number, I wonder if this might work? 由于spplot(zcol = ...可以是列号,我想知道这是否可行?

traits <- c("height","bmi","whr","body_fat","income")
for(i in 1:length(traits)) {
  pdf(paste0("plot_of_", traits[i], ".pdf")
  spplot(MyData, i, col.regions = my.palette, cuts = 8, lwd = 0.5)
  dev.off()
}

Without looking at your data, I cannot say why you have the undefined column error. 如果不查看数据,我无法说出为什么您有未定义的列错误。 Replicating your 'UPDATE 2' code with the meuse dataset does not throw the same error for me: 用meuse数据集复制“ UPDATE 2”代码对我不会引发相同的错误:


library(sp)
demo(meuse, ask = FALSE, echo = FALSE)
TRAIT <- "copper"
my.palette <- c("black", "red", "green", "blue")
spplot(meuse, TRAIT, col.regions = my.palette, cuts = 8, lwd = 0.5)

As for the first case of your question: spplot (much like ggplot ) won't output anything to the device from within a loop unless explicitly printed. 对于您的问题的第一种情况: spplot (与ggplot )不会从循环内将任何内容输出到设备,除非显式打印。 Basically you are opening a pdf file, creating the plot but not saving it to the pdf file, then 基本上,您要打开一个pdf文件,创建图,但不将其保存到pdf文件中,然后

The following code should work for you: 以下代码将为您工作:

for (TRAIT in c("height","bmi","whr","body_fat","income")){
    outfile <- paste0("plot_of_", TRAIT, ".pdf")
    pdf(outfile)
    print(spplot(MyData, TRAIT, col.regions = my.palette, cuts = 8, lwd = 0.5))
    dev.off()
}

I tested it using the Meuse dataset and it worked fine there: 我使用Meuse数据集对其进行了测试,并在其中正常工作:

library(sp)
demo(meuse, ask = FALSE, echo = FALSE)
my.palette <- c("black", "red", "green", "blue")
for(TRAIT in c("cadmium", "copper", "lead", "zinc")){
  outfile <- paste0("plot_of_", TRAIT, ".pdf")
  pdf(outfile)
  print(spplot(meuse, TRAIT, col.regions = my.palette, cuts = 8, lwd = 0.5))
  dev.off()
}

Giving me 4 distinct pdfs, each containing the spatial plot of the corresponding variable. 给我4个不同的pdf,每个pdf包含相应变量的空间图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM