0

I'm trying to develop script that will generate random 5x5 matrices (see code), display via ggplot2, and export the object placements via png(). I have the foundations down, but where (I think) I'm running into problems is exporting the images

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

  }
}

Depending on where I run the script, I run into different errors:

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

or

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

I'm having a hard time troubleshooting what it is that's wrong with my format, so any help in understanding the errors is much appreciated.

Connor
  • 39
  • 5
  • You didn't exactly ask a question here. What is the problem you are trying to solve? How is the behavior you are seeing different from the one you want? – MrFlick Jul 17 '17 at 20:54
  • Possible duplicate: https://stackoverflow.com/questions/15678261/r-ggplot-does-not-work-if-it-is-inside-a-for-loop-although-it-works-outside-of – MrFlick Jul 17 '17 at 20:55
  • @ChiPak Trying to export each individual one for use as an experimental stimulus – Connor Jul 17 '17 at 21:00
  • What are you trying to do with `grid.edit`? The rest of your code works if you leave it out. – CPak Jul 17 '17 at 21:23
  • @ChiPak I'm increasing the thickness of the shapes in the plot, it's my first time using it so I'm very green with its functionality – Connor Jul 17 '17 at 21:32

1 Answers1

0

---LINE THICKNESS---

You can use stroke to control line thickness of your shapes

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

Then you don't need to use grid, grid.edit, grid.force (delete these commands).

----SAVE PLOTS-----

You can save your ggplots with ggsave. Place ggsave right after ggplot

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

----SUGGESTIONS FOR SIMPLIFICATION----

You can also simplify your make-random-matrix commands with:

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

which can replace:

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
CPak
  • 13,260
  • 3
  • 30
  • 48