R and bitmap: lines around each cell
I want to have black lines around every cell in the raster. Below is the example data. I mean, instead of these frames / borderless
I want this (cells with borders)
library(raster)
require(graphics)
require(grDevices)
library(colorRamps)
data<-matrix(c(1,0.4,0.5,0.8,-0.9,0.3,-0.89,-0.62,-0.33),ncol=3)
r <- raster(nrows=dim(data)[1],ncols=dim(data)[2],
xmn=-1,xmx=1,ymn=-1,ymx=1)
r[]<-data
setValues(r,factor(data))
plot(r,col=c(topo.colors(200)),axes=FALSE,box=FALSE)
+3
source to share
2 answers
If you are open to using ggplot2, I suggest a possible solution using geom_tile()
. Pixels can be delineated using the argument colour
for geom_tile
. The downside is that your data might need some reformatting to use with ggplot2.
library(ggplot2)
library(reshape2)
dat = melt(volcano[26:40, 26:40])
p = ggplot(dat, aes(x=Var1, y=Var2, fill=value)) +
geom_tile(colour="grey20") +
scale_fill_gradientn(colours = terrain.colors(10))
ggsave("tile_plot.png", plot=p, height=6, width=7, dpi=150)
+2
source to share