Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R在循环中时不遵守ggplot2文件保存说明_R_Loops_Ggplot2_Graphics - Fatal编程技术网

R在循环中时不遵守ggplot2文件保存说明

R在循环中时不遵守ggplot2文件保存说明,r,loops,ggplot2,graphics,R,Loops,Ggplot2,Graphics,我有一个循环,迭代两次,每次迭代生成一个图形并将其写入png文件。一直到昨天。然后,昨天,在做了一些不相关的更改(我不记得是什么)后,它停止了工作。具体来说,它不会崩溃,甚至不会生成警告,它只是不会根据请求创建png文件 但是,如果我手动将循环变量I设置为1或2,然后选择并仅执行循环中的代码,它就会工作 我弄不明白为什么会发生这种奇怪的行为。我认为这可能与Rstudio有关,但我在没有Rstudio的情况下尝试了它,既从linux命令行运行,又通过source()命令从R命令行运行,在这两种情况

我有一个循环,迭代两次,每次迭代生成一个图形并将其写入png文件。一直到昨天。然后,昨天,在做了一些不相关的更改(我不记得是什么)后,它停止了工作。具体来说,它不会崩溃,甚至不会生成警告,它只是不会根据请求创建png文件

但是,如果我手动将循环变量I设置为1或2,然后选择并仅执行循环中的代码,它就会工作

我弄不明白为什么会发生这种奇怪的行为。我认为这可能与Rstudio有关,但我在没有Rstudio的情况下尝试了它,既从linux命令行运行,又通过source()命令从R命令行运行,在这两种情况下,结果都是一样的

我希望有人能提出问题所在

我在64位PC上的LinuxMint上通过Rstudio版本1.0.136使用R版本3.3.1。使用的R软件包包括gridExtra、gridGraphics、GTTable和png

以下是循环:

for (i in 1:2){
  temp<-subset(graph.dat,qtr.index==i)
  png(paste(output_dir,'3.',c('a','b')[i],'.G.png',sep=''),width=1800,height=1152)  
# Open file to write graph to. Save as 3.a.G in first iteration and 3.b.G in the second
  print(i)
  ggplot() +
    geom_bar(data = temp, aes(x=Customer, y=pmax(0,Amount), fill=Category),stat = "identity",width=0.5) + 
# this line plots the bars above the zero line
    geom_bar(data = temp, aes(x=Customer, y=pmin(0,Amount), fill=Category),stat = "identity",width=0.5) + 
# this line plots the bars below the zero line
    scale_fill_brewer(type = "seq", palette = "Paired") +
    labs(x='',y='Provision for tax')+
    coord_cartesian(ylim = c(-15000, 15000))+
    geom_point(data=subset(Estimate,qtr.index==i), aes(x=Customer, y=Amount),size=13,shape=15)+ 
# this line plots the point markers
    ggtitle(paste('Stocktake - ',dates.df$dates.short[i],sep=''))
  dev.off() 
# Now save the graph to a .png file
}

在for循环的主体中禁用了自动打印,因此不会实际创建图形。您需要手动打印ggplot对象。我相信这是重复的…请阅读R-FAQ第7节的项目编号。。。大于20小于35,但你应该把它们全部读一遍,而不是让我查一下。。。。同样。@andrew kirk您需要共享其他变量Estimate、dates.df等的数据。。。正如Joshua所指出的,你需要将绘图存储在变量
p中,非常感谢Joshua和Karthik。这解决了问题。
"Category"  "Customer"  "qtr.index" "Amount"
"Stock" "Cust 1"    "2" -2178.34975
"Other Expenses"    "Cust 1"    "2" -1307.11426
"Other Income"  "Cust 1"    "2" -4556.12196
"Revenue"   "Cust 1"    "2" 2797.16517
"Other Expenses"    "Cust 1"    "2" 4283.00036
"Receivables"   "Cust 1"    "2" 388.23353
"Stock" "Cust 3"    "2" -133.7925
"Other Expenses"    "Cust 3"    "2" -1056.23411
"Other Income"  "Cust 3"    "2" -46.64558
"Revenue"   "Cust 3"    "2" 75.5956
"Other Expenses"    "Cust 3"    "2" 1747.73888
"Receivables"   "Cust 3"    "2" -625.0622
"Stock" "Cust 4"    "2" -366.3761465
"Other Expenses"    "Cust 4"    "2" -42.9030924
"Other Income"  "Cust 4"    "2" -225.5160776
"Revenue"   "Cust 4"    "2" 150.7087336
"Other Expenses"    "Cust 4"    "2" 7.2424856
"Receivables"   "Cust 4"    "2" 489.100618
"Stock" "Cust 6"    "2" -1944.27337910587
"Other Expenses"    "Cust 6"    "2" -719.491754021401
"Other Income"  "Cust 6"    "2" -3506.80597312394
"Revenue"   "Cust 6"    "2" 2377.06949826214
"Other Expenses"    "Cust 6"    "2" 6272.82876949579
"Receivables"   "Cust 6"    "2" -3128.20591286837
"Stock" "Cust 7"    "2" -3143.20946024247
"Other Expenses"    "Cust 7"    "2" -1191.70611887001
"Other Income"  "Cust 7"    "2" -4947.00046655931
"Revenue"   "Cust 7"    "2" 3672.57987260769
"Other Expenses"    "Cust 7"    "2" 5057.1723408229
"Receivables"   "Cust 7"    "2" -1777.46732478221
"Stock" "Cust 5"    "2" -3096.57633
"Other Expenses"    "Cust 5"    "2" -1489.33705
"Other Income"  "Cust 5"    "2" -1788.19689
"Revenue"   "Cust 5"    "2" 2586.39059
"Other Expenses"    "Cust 5"    "2" 1974.36526
"Receivables"   "Cust 5"    "2" 754.04481
"Stock" "Cust 1"    "1" -2248.1011
"Other Expenses"    "Cust 1"    "1" -1406.18579
"Other Income"  "Cust 1"    "1" -4804.24372
"Revenue"   "Cust 1"    "1" 3082.00792
"Other Expenses"    "Cust 1"    "1" 4621.07184
"Receivables"   "Cust 1"    "1" 49.54561
"Stock" "Cust 3"    "1" -125.23352
"Other Expenses"    "Cust 3"    "1" -1010.1423
"Other Income"  "Cust 3"    "1" -50.4339
"Revenue"   "Cust 3"    "1" 75.24065
"Other Expenses"    "Cust 3"    "1" 1604.92511
"Receivables"   "Cust 3"    "1" -558.1197
"Stock" "Cust 4"    "1" -353.344313
"Other Expenses"    "Cust 4"    "1" -41.970828
"Other Income"  "Cust 4"    "1" -195.553684
"Revenue"   "Cust 4"    "1" 154.670712
"Other Expenses"    "Cust 4"    "1" 5.998472
"Receivables"   "Cust 4"    "1" 432.343192
"Stock" "Cust 6"    "1" -1995.81268914429
"Other Expenses"    "Cust 6"    "1" -748.13574488012
"Other Income"  "Cust 6"    "1" -3420.80676167929
"Revenue"   "Cust 6"    "1" 2491.28703802434
"Other Expenses"    "Cust 6"    "1" 5932.08646545911
"Receivables"   "Cust 6"    "1" -2980.3132874941
"Stock" "Cust 7"    "1" -3176.99364830721
"Other Expenses"    "Cust 7"    "1" -1258.21862586926
"Other Income"  "Cust 7"    "1" -5264.98717587913
"Revenue"   "Cust 7"    "1" 4184.60759887337
"Other Expenses"    "Cust 7"    "1" 5555.32857632657
"Receivables"   "Cust 7"    "1" -2362.99537101079
"Stock" "Cust 5"    "1" -3149.18544
"Other Expenses"    "Cust 5"    "1" -1521.17289
"Other Income"  "Cust 5"    "1" -2032.76354
"Revenue"   "Cust 5"    "1" 2807.38446
"Other Expenses"    "Cust 5"    "1" 2236.51582
"Receivables"   "Cust 5"    "1" 677.97416