Search code examples
rggplot2vector-graphicssmoothing

Turn pixel image into scalable vector graphic in ggplot


Please note that im not interested in any kind of interpolation algorithms where you expand the amount of pixels and interpolate the new values.

I want to leave the world of pixel based images and am looking for some scalable vector image solution.

Is there a way to turn a pixel image in a ggplot into a color meshed smooth vector graphic?

The following pictures demonstrate what im aiming for.

enter image description here

and then smooth it out.

enter image description here

The images are taken from the following wikipedia article HERE

Please note that the original images from the article are SVG files. You can zoom in as much as you want and you always have smooth color transitions and no edges.

Some additional images and infos: HERE2

Here is some example Data of something that meets the first image "nearest"

library(ggplot2)
n = 5
pixelImg <- expand.grid(x=1:n,y=1:n)
pixelImg$value <- sample(1:n^2,n^2,replace = T)

ggplot(pixelImg, aes(x, y)) +
  geom_raster(aes(fill = value)) +
  scale_fill_gradientn(colours=c("#FFCd94", "#FF69B4", "#FF0000","#4C0000","#000000"))

If not in ggplot is there a way to do it outside of ggplot?


Solution

  • Look into the ggsave() function. It supports .svg files for vector graphics.