How to build a crossword storyline for a boolean matrix

I have a boolean matrix:

mm <- structure(c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE), .Dim = c(10L, 10L), .Dimnames = list(NULL, c("n1", "n2", "n3", "n4", "n5", "n1.1", "n2.1", "n3.1", "n4.1", "n5.1"))) 

For this matrix, I would like to make a plot similar to this:

crossword-like plot

(the picture is taken from a similar question for Matlab: How can I display a two-dimensional binary matrix as a black and white graph? )

Maybe I am missing something obvious, but I don’t see an easy way to do this in R. So far, my best attempt is based on barplot:

 m1 <- matrix(TRUE,ncol=10,nrow=10) barplot(m1,col=mm) 

but all lines have the same colors.

Any ideas are welcome

+5
source share
4 answers

You can do this using ggplot2 geom_tile and reshape2 melt :

 library(ggplot2) library(reshape2) melted <- melt(mm) ggplot(melted, aes(x = Var2, y = Var1, fill = value)) + geom_tile() + scale_fill_manual(values = c("white", "black")) 

To make it a little more neat, you can remove the legend and gray edges with some changes to the theme:

 ggplot(melted, aes(x = Var2, y = Var1, fill = value)) + geom_tile() + scale_fill_manual(values = c("white", "black")) + theme_bw() + theme(legend.position = "none") 

Final conclusion:

enter image description here

+5
source

Here's an approach that uses the qheat shell for ggplot2 from the qdap package:

 library(qdap) qheat(t(data.frame(mm)), by.column=NULL, high="black", text.color =NA, grid="grey20") + guides(fill=FALSE) 

enter image description here

+5
source

Here are a few more approaches for rounding down graphic options.

  • basic graphics with rect :

     plot.new() par(mar=rep(0, 4)) plot.window(xlim=c(0, ncol(mm)), ylim=c(0, nrow(mm)), asp=1) o <- cbind(c(row(mm)), c(col(mm))) - 1 rect(o[, 1], o[, 2], o[, 1] + 1, o[, 2] + 1, col=t(mm)[, ncol(mm):1]) 

    enter image description here

  • lattice::levelplot and latticeExtra :

     library(latticeExtra) levelplot(t(mm)[, ncol(mm):1], asp=1, scales='sliced', col.regions=c('white', 'black'), margin=FALSE, colorkey=FALSE) + layer(panel.grid(h=nrow(mm)-1, v=ncol(mm)-1, col=1)) 

    enter image description here

  • rasterVis::levelplot , raster and latticeExtra :

     library(rasterVis) library(latticeExtra) levelplot(raster(mm), col.regions=c('white', 'black'), margin=FALSE, colorkey=FALSE) + layer(panel.grid(h=nrow(mm)-1, v=ncol(mm)-1, col=1)) 

    enter image description here

  • sp::spplot , raster and latticeExtra :

     library(raster) library(latticeExtra) spplot(raster(mm), colorkey=FALSE, col.regions=c('white', 'black')) + layer(panel.grid(h=nrow(mm)-1, v=ncol(mm)-1, col=1)) 

    enter image description here

  • raster

    It is hardly possible to set the dimensions of the graphics device in such a way that the raster does not display additional (partial) cells outside the expected limits x and y. When using this approach, if the goal is to export to, for example, png, I first draw in windows / x11 / quartz and resize the window until the plot area is as I expected, then query the device sizes using dev.size() and use these values ​​to determine the aspect ratio for plotting png .

     plot(raster(mm), legend=FALSE, col=c('white', 'black')) abline(h=seq(0, 1, len=nrow(mm) + 1), v=seq(0, 1, len=ncol(mm) + 1)) 

    enter image description here

+3
source

The plot method from the graphics package is used here:

 plot(rep(1:10, each = 10), rep(-(1:10), 10), axes = FALSE, ann = FALSE, pch = ifelse(mm, 0, 15), cex = 6, asp = 1, xpd = NA) 

enter image description here

+2
source

Source: https://habr.com/ru/post/1211454/


All Articles