2

When using plotrix functions to generate a smooth gradient, I get banding patterns in the gradient on a mac, but see no such artifacts on a linux system. I'm looking for suggestions that may explain why this happens and how to fix it on MacOSX.

Below is some code that generates a color gradient in a png file, and then uses it as the background for a plot. The results look like the below image - left is mac, right is linux: banding pattern in gradient

Code that produces the image:

library(png)
library(plotrix)

##create a png with a smooth background gradient
createBackgroundImage <- function(col=c("bisque","darkgoldenrod1","darkorange3")){
    tmpfile=tempfile()
      png(tmpfile,width=80,height=80)
      par(mar=c(0,0,0,0))
      plot(-100,-100,col="white",ylim=c(0,100), xlim=c(0,100),
                  yaxt="n", xaxt="n",xlab="",ylab="",bty="n")

      ##background color
      plotrix::gradient.rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
                                                      col=plotrix::smoothColors(col[1],100,col[2],50,col[3],alpha=200),
                                                      border=NA)
      dev.off()
      return(tmpfile)
  }


## use that png as the background for a plot
pdf("test.pdf",height=4,width=4)
bckImage = png::readPNG(createBackgroundImage())
##set up the plot
  plot(-100,-100,col="white",
              ylim=c(0,100),
              xlim=c(0,100),
              yaxt="n", xaxt="n",
              bty="n", xlab="", ylab="")

lim=par()
rasterImage(bckImage, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
dev <- dev.off()

I have verified that it isn't just a display issue. (Viewing the output pdfs on different systems preserves the banding or lack thereof). Both systems are running R 3.2.1 and the relevant packages (plotrix and png) are up-to-date. (plotrix_3.6-1 and png_0.1-7 on both systems)

Suggestions on what's going on here and how I might get rid of this banding would be much appreciated!

chrisamiller
  • 2,712
  • 2
  • 20
  • 25
  • Update: reducing the number of colors input seems to ameliorate the problem, but I'd still love to know what's going on to cause the difference between platforms. – chrisamiller May 10 '16 at 19:08

0 Answers0