I have a boolean matrix:
mm <- structure(c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRU
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:
Here are a few more approaches to round out the graphics options.
base 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])
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))
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))
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))
raster
It can be a bit fiddly to set the graphics device's dimensions such that raster
doesn't plot additional (partial) cells outside the intended x and y limits. When using this approach, if the goal is to export to e.g. png, I plot to windows
/x11
/quartz
first and resize the window until the plotted area is as I'd intended, then query the device dimensions with dev.size()
and use these values to determine the dimension ratio for plotting to 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))
Here's an approach that uses qheat
a wrapper 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)
Here's an approach using plot
from the graphics
package:
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)