To build this, you will need three data points:
x, y, color
Thus, the first step is restructuring.
Fortunately, matricies is already a vector, just with a dimension attribute, so we just need to create data.frame x, y coordinates. We do this with help expand.grid.
mat <- matrix(round(runif(900-30, 0, 5),2), 30)
(x, y) data.frame.
, y - seq x seq
dat <- expand.grid(y=seq(nrow(mat)), x=seq(ncol(mat)))
dat <- data.frame(dat, value=as.vector(mat))
dat$color <- cut( dat$value,
breaks=c(-Inf, 1, 2, Inf),
labels=c("green", "yellow", "red")
)
library(ggplot2)
ggplot(data=dat, aes(x=x, y=y)) + geom_point(color=dat$color, size=7)
