R Barchar does not work in For loop

流过昼夜 提交于 2019-12-12 06:57:00

问题


I am using barchart in For loop that goes on years, the barchart command is ok outside the loop, but inside - something goes wrong.

Here is my code:

for(i in 2006:2016){ 
  htr2 = htr[htr$year==i , ]
  barchart(year ~ y_kayam , groups=shlav, htr2, auto.key = list(columns = 2)  )
  Sys.sleep(5) }

Here is my data

year    vaada   shlav   tm38    y_kayam y_mevukash
2013    410         1   1       12.00
2013    410         2   1       12.00
2013    410         1   1       
2013    410         1   1       8.00    15.00
2013    410         3   1       8.00    15.00
2013    410         1           8.00    15.00
2013    410         1   1      15.00    28.00
2013    410        3    1      15.00    28.00

Thanks!


回答1:


For plotting the six plots nicely, I would use the par function.

par(mfrow=c(3,3))

for(i in 2006:2016){ 
  htr2 = htr[htr$year==i , ]
  barchart(year ~ y_kayam , groups=shlav, htr2, auto.key = list(columns = 2)  )
  Sys.sleep(5) }

Now, you can use dev.copy() to save it as a PNG (or another image type).

dev.copy(png, 'myPlots.png')
dev.off()


来源:https://stackoverflow.com/questions/41034164/r-barchar-does-not-work-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!