使用spplot在R中的for循环中绘制图 [英] Make plots in a for loop in R with spplot

查看:233
本文介绍了使用spplot在R中的for循环中绘制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用R语言编写程序时遇到了一些麻烦,通常我会在另一种语言的for循环中进行此操作...

I'm having a little trouble programming stuff in R that I would normally do in a for loop in another language...

我现在正在尝试使用sp程序包中的spplot绘制图(这是一个chloropleth映射图),但是我想对一堆变量进行绘制.在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. In a for loop, I would program it like this:

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()
}

每个TRAIT实例应替换为我提供给for循环的变量(因此在pdf文件名中,并作为spplot命令中的参数).现在,显然这段代码不起作用,但是如何在R中做到这一点呢?

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). Now, obviously this code does not work, but how would one go about doing this in R?

更新:

我获得了创建不同pdf文件的代码,但是spplot命令仍然无法使用.它会创建空的pdf文件:

I got the code to create different pdf files, but the spplot command still will not work. It creates empty pdf files:

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()
}

如果我在for循环之外运行完全相同的代码,如下所示:

If I run the exact same code outside of the for loop like this:

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

它工作正常,并且我在文件plot_of_height.pdf中获得了所需的图.

it works fine and I get the desired plot in the file plot_of_height.pdf.

更新2: 这似乎是spplot的问题.如果我运行此命令:

UPDATE 2: This seems to be an issue with 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()

我收到以下错误:

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

推荐答案

在不查看数据的情况下,我无法说出为什么您有未定义的列错误.用meuse数据集复制"UPDATE 2"代码不会对我造成相同的错误:

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:


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)

对于您的问题的第一种情况:spplot(与ggplot相似)不会在循环内将任何内容输出到设备,除非明确打印.基本上,您要打开一个pdf文件,创建图,但不将其保存到pdf文件中,然后

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. Basically you are opening a pdf file, creating the plot but not saving it to the pdf file, then

以下代码应为您工作:

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()
}

我使用Meuse数据集对其进行了测试,并在其中正常工作:

I tested it using the Meuse dataset and it worked fine there:

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()
}

给我4个不同的pdf,每个pdf包含相应变量的空间图.

Giving me 4 distinct pdfs, each containing the spatial plot of the corresponding variable.

这篇关于使用spplot在R中的for循环中绘制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆