简体   繁体   中英

Why does my plot of a raster in R blur in saved file?

I'm hoping to export a map with a raster plotted on it as a .pdf or some other vector file. For some reason, when I save it as a .pdf or .ps file, the raster is blurry/fuzzy/smeared and unusable. Any tips for fixing this? A .png saves with the desired resolution, but can't be manipulated layer-by-layer in post-processing.

library(maps)
library(raster)

ra<-raster(nrows=40,ncols=35,xmn=-110,xmx=-75,ymn=7,ymx=47)
ra2 <- rasterize(cbind(runif(1000,min=-109,max=-76),runif(1000,min=8,max=46)), ra,fun=function(x,...)log10(length(x)))
pal <- colorRampPalette(c("grey90","grey40"))
map(database="world",regions=c("US","Mexico","El Salvador","Honduras","Costa Rica","Guatemala","Belize","Nicaragua","Panama"),myborder=0.0000001,xlim=c(-110,-75),ylim=c(0,50))
plot(ra2,add=T,col=pal(7))

Here is a screenshot of the saved .pdf file
Here is a screenshot of the Quartz window with desired resolution

我刚刚发现在 plot() 函数中添加 'useRaster=FALSE' 可以解决这个问题。

You can use the following code to save high-resolution image as pdf file

pdf(file = "My Plot.pdf",   
    width = 14, # The width of the plot in inches
    height = 8.5) # The height of the plot in inches

pal <- colorRampPalette(c("grey90","grey40"))
map(database="world",regions=c("US","Mexico","El Salvador","Honduras","Costa Rica","Guatemala","Belize","Nicaragua","Panama"),myborder=0.0000001,xlim=c(-110,-75),ylim=c(0,50))
plot(ra2,add=T,col=pal(7))

#Run dev.off() to create the file!
dev.off()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM