How to smooth a graph created using the image function in R?

I am creating a height map with the base function R "image". However, I would like to get a smoother transition between the levels so that the image looks less pixelated. The logical solution is to add a wider color palette, which I have already done, but the result is not yet satisfactory. How can I achieve a nice smoothing effect without changing the coordinate system (I need it to overlap 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))
+4
source share
1 answer

For some reason, interpolate=FALSEhard coded in image.default, but you can use the lower level function rasterImagedirectly:

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)

:

enter image description here

+5

Source: https://habr.com/ru/post/1681489/


All Articles