问题
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