I have a reasonably high definition global map raster and want to plot to file, but cannot seem to maintain resolution. Plotting a restricted region works ok, but the whole world always ends up with reduced resolution, no matter what method I've used. Am I missing something here? I've often output high-res rasters before without this problem, but I'm unable to identify the cause in this case. To illustrate:
require(raster)
require(rworldmap)
data(countriesCoarse); worldmap = countriesCoarse
worldmap@data = data.frame(x = rep(1,length(worldmap))) # uniform vector
x_res = 3600; y_res = 1800
r0 <- raster(extent (-180,180,-90,90), ncol=x_res, nrow=y_res, crs=crs(worldmap))
rastermap <- rasterize(worldmap, r0, 'x') # ~ 2 mins
# small-area plot confirms raster's data resolution is ok
plot(rastermap, col='grey', xlim=c(-10,5), ylim=c(49,60), asp=T, axes=F, box=F, legend=F)
# the resolution is maintained for limited-area plot to file
png('plot0.png', width=x_res, height=y_res)
plot(rastermap, col='grey', axes=F, box=F, legend=F, xlim=c(-10,5), ylim=c(49,60), asp=T)
dev.off(); browseURL('plot0.png')
# but outputting global plots loses resolution..
par(mai=c(0,0,0,0))
png('plot1.png', width=x_res, height=y_res)
plot(rastermap, col='grey', axes=F, box=F, legend=F)
dev.off(); browseURL('plot1.png')
png('plot2.png', width=x_res, height=y_res)
image(rastermap, col='grey')
dev.off(); browseURL('plot2.png')
pdf('plot3.pdf', width=24, height=12)
plot(rastermap, col='grey', axes=F, box=F, legend=F)
dev.off(); browseURL('plot3.pdf')
require(Cairo)
CairoPNG('plot4.pdf', width=x_res, height=y_res)
plot(rastermap, col='grey', axes=F, box=F, legend=F)
dev.off(); browseURL('plot4.pdf')
This is how output plots to file typically come out (same resolution outputs):
try ?plot
, read the plot method provided by package raster
, and find out about the maxpixels
argument. Then, try e.g.
plot(rastermap, col='grey', axes=F, box=F, legend=F, maxpixels=1e8)
or
plot(rastermap, col='grey', axes=F, box=F, legend=F, maxpixels= x_res * y_res)
faster is to have the the graphics device do the rasterization:
require(Cairo)
CairoPNG('plot4.png', width=x_res, height=y_res)
plot(worldmap, col = 'grey', border = 'grey')
dev.off();
browseURL('plot4.png')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With