简体   繁体   中英

How to smooth a plot produced with image function in R?

I'm producing an elevation map with R base function "image". Nonetheless, I'd like to get a smoother transition between levels so the image looks less pixelated. The logical solution is adding a wider color palette, which I already did, but the result isn't satisfactory yet. How can I achieve a nice smoothing effect without altering the coordinate system (I need it to overlay a series of segments and points)?

x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
shades = colorRampPalette(c("yellow", "red"))
image(x, y, volcano,
col=shades(100),breaks=seq(min(volcano),max(volcano),
(max(volcano)- min(volcano))/100),axes = FALSE)
segments(x0=c(100,600), x1=c(600,100), 
     y0=c(100,600),
     y1=c(600,100))
points(x=seq(100,800,100), y=rep(300,8),pch=16,col="blue",cex=3)
axis(1, at = seq(100, 800, by = 100))
axis(2, at = seq(100, 600, by = 100))

For some reason interpolate=FALSE is hard-coded in image.default , but you can use the lower-level rasterImage function directly:

m <- cut(scales::rescale(volcano), 100)
levels(m) <- shades(100)
m <- as.character(m)
dim(m) <- dim(volcano)
m <- t(m)[ncol(m):1,]

rasterImage(as.raster(m), min(x), min(y), max(x), max(y), interpolate = TRUE)

Side-by-side comparison:

在此输入图像描述

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