This question already has an answer here:
这个问题在这里已有答案:
- ggplot's qplot does not execute on sourcing 1 answer
ggplot的qplot在执行1个答案时不执行
today while trying to make a R script to plot several maps in a folder, I came to a situation that I can't understand and/or fix. I wrote the following code, that would be the script to plot 1 map per each 2 weeks during 31 periods (62 weeks), the script works fine if I run it alone (without the loop) but as soon I place the loop, there will be no files created and the script ends inmediately (and all the variables are changed as if it had run).
今天,当我试图制作一个R脚本来在一个文件夹中绘制几个地图时,我遇到了一种我无法理解和/或修复的情况。我写了下面的代码,这将是在31个时期(62周)内每2周绘制1个地图的脚本,如果我单独运行(没有循环),脚本工作正常,但是一旦我放置循环,那里将不会创建任何文件,并且脚本会立即结束(并且所有变量都会更改,就像它已运行一样)。
Note that raw is a dataset (as I said the code works if the loop is not there, for only 1 iteration), the al1 is the map definition that is defined as well.
请注意,raw是一个数据集(正如我所说,如果循环不存在,代码可以工作,只进行1次迭代),al1也是定义的地图定义。
date_actual <- as.Date("2012-01-01")
i <- 0
for(i in 1:31){
conc <- subset(raw,(raw$Day >= date_actual & raw$Day < (14+date_actual)))
png(filename=paste("map_",date_actual,".png",sep=""), width = 1920, height = 1080, units = "px")
ggmap(al1) + geom_point(data = conc, aes(x = Lon, y = Lat, size = Mag), colour="red", alpha = .5) + scale_size_area(max_size=8, name="Magnitude",breaks=c(1,3,5,7,9)) + ggtitle(paste("",format(date_actual, "%Y-%U"),sep="")) + theme(plot.title = element_text(lineheight=1.2, face="bold"))
date_actual <- date_actual + 14
dev.off()
}
1 个解决方案
#1
This is FAQ 7.22 (but is less obvious with ggplot2 graphs, since they are not mentioned in the question, only the answer).
这是常见问题7.22(但对于ggplot2图表不太明显,因为问题中没有提到它们,只有答案)。
Basically ggplot2 graphs (and lattice graphs, and some others) don't actually plot until they are print
ed. Outside of the loop the autoprinting in R did this for you, but autoprinting does not happen inside of loops, so R never was told to actually create the plots.
基本上ggplot2图形(和图形图形,以及其他一些图形)在打印之前不会实际绘图。在循环之外,R中的自动打印为您做了这个,但是自动打印不会发生在循环内部,因此R从未被告知实际创建绘图。
The solution is to wrap the line that creates the graph in print()
or plot()
.
解决方案是在print()或plot()中包装创建图形的线。
#1
This is FAQ 7.22 (but is less obvious with ggplot2 graphs, since they are not mentioned in the question, only the answer).
这是常见问题7.22(但对于ggplot2图表不太明显,因为问题中没有提到它们,只有答案)。
Basically ggplot2 graphs (and lattice graphs, and some others) don't actually plot until they are print
ed. Outside of the loop the autoprinting in R did this for you, but autoprinting does not happen inside of loops, so R never was told to actually create the plots.
基本上ggplot2图形(和图形图形,以及其他一些图形)在打印之前不会实际绘图。在循环之外,R中的自动打印为您做了这个,但是自动打印不会发生在循环内部,因此R从未被告知实际创建绘图。
The solution is to wrap the line that creates the graph in print()
or plot()
.
解决方案是在print()或plot()中包装创建图形的线。