2017-07-17 4 views
0

J'essaie de développer un script qui générera des matrices aléatoires 5x5 (voir code), affichera via ggplot2 et exportera les emplacements d'objets via png(). Je les fondations vers le bas, mais où (je pense) que je suis en cours d'exécution dans des problèmes est l'exportation des imagesGraphique Exportation dans une boucle avec grille

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() 

    } 
} 

Selon l'endroit où je lance le script, je cours dans les différentes erreurs:

Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : 
    unable to start png() device 

ou

Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : 
    'gPath' (geom_point.points) not found 

Je vais avoir du mal dépanne ce qu'il est que ne va pas avec mon format de sorte que toute aide à comprendre les erreurs est très apprécié.

+0

Yo Tu n'as pas vraiment posé de question ici. Quel est le problème que vous essayez de résoudre? Comment le comportement que vous voyez est-il différent de celui que vous voulez? – MrFlick

+0

Copie possible: https://stackoverflow.com/questions/15678261/r-ggplot-does-not-work-if-it-is-inside-a-for-loop-although-it-works-horside-of – MrFlick

+0

@ChiPak Essayer d'exporter chaque individu pour l'utiliser comme stimulus expérimental –

Répondre

0

--- --- EPAISSEUR EN LIGNE

Vous pouvez utiliser stroke pour contrôler l'épaisseur de la ligne de vos formes

geom_point(position=position_jitter(width=.2, height=.2), size=15, stroke=5) 

vous n'avez pas besoin d'utiliser grid, grid.edit, grid.force (supprimez ces commandes).

---- ----- PARCELLES SAUVER

Vous pouvez enregistrer votre ggplots avec ggsave. Placez ggsave juste après ggplot

ggsave(paste0("figs/", d1l,"items-interation",i,".png"), width = 9, height = 9, dpi = 100) 

---- ---- SUGGESTIONS POUR SIMPLIFICATION

Vous pouvez également simplifier votre make-aléatoire commandes de matrice avec:

a5 <- sample(c(rep("X",d1l),rep("O",25-d1l)),replace=F) 
a5[sample(1:25,1)] <- "T" 
a5 <- matrix(a5,nrow=5) 

qui peut remplacer:

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