ランダムな5x5行列(コード参照)を生成し、ggplot2で表示し、png()
でオブジェクトのプレースメントを書き出すスクリプトを開発しようとしています。私は基礎をダウンしていますが、(私は思う)私はグラフィックグリッドでループをエクスポートする
library(ggplot2)
library(grid)
### 5x5 Array ###
for(d1l in c(12, 9, 7)) {
for(i in 1:20) {
a5 <- matrix(nrow = 5, ncol = 5) # Empty 5x5 matrix
a5l <- length(a5) # Number of cells in array
ind <- sample(x = length(a5), replace = F, size = a5l) # Rand. indexes
a5[ind[1:d1l]] <- "X" # Write objects to rand. indexes
a5[ind[d1l+1:24]] <- "O" # Write remaining obj to rand. indexes
a5[ind[25]] <- "T" # Place target
# Turn into data frame for ggplot2
testdf <- data.frame("row"=substr(levels(interaction(1:5, 1:5, sep="")),1,1),
"col"=substr(levels(interaction(1:5, 1:5, sep="")),2,2),
"val"=a5[1:25],
"colour"=ifelse(a5[1:25]=="X", "green", "red"))
# Initiate graphics device
png(paste0("figs/", d1l,"items-interation",i,".png") , height=1080, width=1920)
# Generate plot
ggplot(testdf, aes(x=row, y=col, shape=val, colour=colour))+
geom_point(position=position_jitter(width=.2, height=.2), size=15)+
scale_shape_manual(values=c("O"=1, "X"=4, "T"=4))+
scale_colour_manual(values=c("red"="red", "green"="green4"))+
theme_minimal()+
theme(panel.grid.major = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
legend.position = "none")
# Force editing
grid.force()
# Changing thickness of shapes
grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = 5))
# Turn off gr device
dev.off()
}
}
は私がスクリプトを実行する場所に応じて画像をエクスポートしている問題に実行している場合は、私は別のエラーに遭遇:
Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, :
unable to start png() device
または
Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) :
'gPath' (geom_point.points) not found
私はそれはそれは私のフォーマットと間違っているので、エラーを理解する上で任意のヘルプははるかに高く評価されているもののトラブルシューティングに苦労しています。
ヨーここで質問したことはありませんでした。あなたが解決しようとしている問題は何ですか?あなたが見ている行動は、あなたが望むものとどう違うのですか? – MrFlick
可能な複製:https://stackoverflow.com/questions/15678261/r-ggplot-does-not-work-if-it-is-inside-a-for-loop-although-it-works-outside-of – MrFlick
@ChiPak実験的刺激として使用するために個々のものをエクスポートしようとしています –