I want to export a raster as a PNG image. I don't want borders, labels, legends, axis... all I want is the map covering the whole image.
The data come from here (vegetation cover) and here (administrative boundaries).
library(raster)
library(rgdal)
r <- raster('gm_lc_v3.tif')
p <- readOGR('ne_10m_admin_0_countries','ne_10m_admin_0_countries')
i <- 66 # Dominican Republic
country <- crop(r,p[i,])
png(paste(p$GEOUNIT[i],'.png',sep=''),width=country@ncols,height=country@nrows)
par(mar=c(0,0,0,0))
plot(country,maxpixels=country@ncols*country@nrows)
dev.off()
It saves the file, but adds an undesirable margin to it. I've seen people suggesting other commands, like raster::spplot or rasterVis::levelplot. I tried spplot, but still didn't work. I don't want to install a new package to do what should (in theory?) be very simple.
country@ncols = 883 and country@nrows = 574. That's the size of the final PNG. But since the margins are being added, my image is not representing the raster correctly. I want each pixel in the raster to become a pixel in the image.
That's the image I'm getting (I added a black border to make the white margin visible).