Poor resolution of raster plot when output to file

我只是一个虾纸丫 提交于 2019-12-13 13:33:31

问题


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):


回答1:


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')


来源:https://stackoverflow.com/questions/28366220/poor-resolution-of-raster-plot-when-output-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!