2

I have a Grid with n*m size and pch Symbols from 1-13 with 8 Colors(so 104 different combinations)

Grid empty:

enter image description here

And now I am trying to draw the symbols as pairs randomly onto my grid. My thinking was I create a matrix with the same size as my grid and random integer numbers and a data frame with all combinations of pch and color and draw them on the grid.

My Goal:

enter image description here

row <- 4
col <- 13
n <- row * col
plot.new()
plot.window(xlim = c(1, col), ylim = c(1, row))
grid(nx = col, ny = row, col = "black")
box(lwd = 2)
axis(1, at=1:col,tick=FALSE,las = 1)
axis(2,at = 1:row,tick= FALSE,las = 2)

pch_v <- c(1:13)
col_v <- c(1:8)

board <- matrix(sample(1:n),nrow = row,ncol=col)
unique_combination <- expand.grid(pch_v,col_v)

Thats the point where I am stuck. If somebody got an idea I would appreciate it.

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

1

Shuffle the unique_combination, then plot:

row <- 4
col <- 13
pch_v <- c(1:col)
col_v <- c(1:row)
unique_combination <- expand.grid(pch_v,col_v)

# random sort
# set.seed(1) # if we want to reproduce
ucr <- unique_combination[ sample(nrow(unique_combination)), ]
d <- data.frame(x = 1:col, y = 1:row, pch = ucr$Var1, col = ucr$Var2)

with(d, plot(x, y, col = col, pch = pch, bty = "n"))

enter image description here

If we need to plot X points, subset before the data:

# subset x rows then plot
with(head(d, 6), plot(x, y, col = col, pch = pch, bty = "n"))

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209