0

I am trying to plot this checker pattern:

checker_pattern <- rbind(c(1/3, 2/3),
                         c(2/3, 1/3))
plot(imager::as.cimg(checker_pattern))

According to this site http://www.sumsar.net/blog/2019/01/image-dithering-in-r/, we should get the following outcome, which I also desire:

enter image description here

However, I get this outcome running the exact same code:

enter image description here

Why do R seem to blur or smooth out the colors in the plot? How do I turn off this blurring?

A similar question (R mschart turn off line smoothing) mentions scaling. I do not know if that gives us a clue in this case.

slamballais
  • 3,161
  • 3
  • 18
  • 29
madsR
  • 95
  • 6

1 Answers1

1

Answer

Set the interpolate argument to FALSE (and furthermore, rescale to FALSE):

plot(imager::as.cimg(checker_pattern), interpolate = FALSE, rescale = FALSE)

enter image description here

Rationale

This has nothing to do with R, and everything to do with the imager package (or rather, the cimg object). You are creating an object of the class cimg, and then provoke plot.cimg. That function has an interpolate argument, which defaults to TRUE. This is then fed into the as.raster function downstream, which applies interpolation. By setting interpolate = FALSE you remove this effect.

Following the same procedure, I found that the plot.cimg also has a rescale argument, which defaults to TRUE. Setting it to FALSE correctly displays the colors.

You can find more information about this function by typing ?plot.cimg.

slamballais
  • 3,161
  • 3
  • 18
  • 29