2013-08-20 2 views
1

for 루프에서 여러 개의 격자 플롯을 생성하려고하지만 빈 이미지를 생성합니다 !!!for 루프 - 빈 이미지가 생성되었습니다

for (f in unique(df$month)) { 
    plot.new() 
    bwplot(x ~ country|type, data = df[df$month == f,], panel=function(...) { 
     panel.abline(h=0, col="green") 
     panel.bwplot(...) 
    }) 
    savePlot(paste0("file_", f), "png") 
} 

for 루프의 내부를 "손으로"실행하면 작동하지만 루프에서 작동이 중지됩니다. 왜?

set.seed(123) 
n <- 300 
country <- sample(c("Europe", "Africa", "Asia", "Australia"), n, replace = TRUE) 
type <- sample(c("city", "river", "village"), n, replace = TRUE) 
month <- sample(c("may", "june", "july"), n, replace = TRUE) 
x <- rnorm(n) 
df <- data.frame(x, country, type, month) 

답변

4

첫 번째 부분은 자주 묻는 질문이다 (R 워드 프로세서에서, 등) : 여기

데이터를 생성하는 코드는 경우 상호 작용하지, (mylatticeplot) 인쇄해야합니다.

또한 RStudio에서는 접근 방식이 작동하지 않습니다.

Error in savePlot(paste0("file_", f), "png") : 
    can only copy from 'windows' devices 

권장되는 방법은 잘 작동하고, 적은 작업입니다 :

png("file_%03d.png") 
for (f in unique(df$month)) { 
    p = bwplot(x ~ country|type, data = df[df$month == f,], panel=function(...) { 
    panel.abline(h=0, col="green") 
    panel.bwplot(...) 
    }) 
    print(p) 
} 
dev.off() 
+0

Aaaaaargh! 얼마나 contventuitive R! 고마워, 나는이 접근법을 보았지만'savePlot'과는 달리 동시에 그 그림을 보여주지는 않을 것이다. – TMS