How to build a crossword-like plot for a boolean matrix

后端 未结 4 1925
暖寄归人
暖寄归人 2020-12-10 15:39

I have a boolean matrix:

mm <- structure(c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, 
                  FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRU         


        
4条回答
  •  盖世英雄少女心
    2020-12-10 16:11

    You can do this using ggplot2's geom_tile and reshape2's 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 bit neater, you could remove the legend and the gray edges with some adjustments 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 output:

    enter image description here

提交回复
热议问题