White lines in gradient.rect from R's plotrix package

572 views Asked by At

Anybody have a solution for removing the white lines seen here in the graduated rectangles at the top of the plot? These are produced with gradient.rect from the plotrix package.

gradient.rect(as.Date("1979-05-04"),70,as.Date("1990-11-28"),80,col=smoothColors("royalblue",100.0,"white"),border=NA,gradient="x")

They appear both in the output from RStudio and in the exported PNG.

R base plot with graduated color rectangles

1

There are 1 answers

1
IRTFM On BEST ANSWER

I was able to reproduce the problem (although not that image) with this code:

png();plot(x=seq(as.Date("1979-05-04"),as.Date("1990-11-28"),length=50),
           1:50,type="n",axes=FALSE)  # set up plot coords correctly
plotrix::gradient.rect(as.Date("1979-05-04"),5, as.Date("1990-11-28"),6, 
                      col=plotrix::smoothColors("royalblue", 100.0,"white"), 
                      border=NA,gradient="x",nslices=121); 
dev.off()

Then I saw three thin, white vertical bands. I looked at the RGB values and plotted their hexadecimal equivalents:

plot(NA, xlim=c(1,110),ylim=c(0x4169E1, 0xFFFFFF) )
lines(1:102, as.numeric(
              paste0('0x', 
                     substr( plotrix::smoothColors("royalblue",100.0,"white"),
                             2,7)
                    ) ) )

I noticed that there were uneven gaps in the sequences so tried using smaller number of colors with better success:

png(); plot(x=seq(as.Date("1979-05-04"),as.Date("1990-11-28"),length=50),1:50,type="n",axes=FALSE)
plotrix::gradient.rect(as.Date("1979-05-04"),20,as.Date("1990-11-28"),30,
       col=plotrix::smoothColors("royalblue",55,"white"),border=NA,gradient="x");dev.off()

enter image description here

I don't really understand why this happens. You might contact Jim Lemon if you need an explanation ... rather than just a fix strategy. His email address should appear at your R console if you type: maintainer("plotrix")