I am struggling to get a visually acceptable color gradient in R (see here for a detailed description of my particular case). The problem, in short, is that while output in the R window looks OK, PDFs show thin, white lines between segments used to generate the gradient.
n <- 100
cc <- colorRampPalette(c("red", "blue"))(n)
plot.new()
par(mar=rep(0,4))
sapply(1:n, function(i) rect((i-1)/n, 0, i/n, 1, col=cc[i], border=NA))
dev.copy2pdf(file="test.pdf")
Here is the result:
You can see the thin, white lines. Their positioning depends on the zoom, so I assume that they are an artifact of how the PDF is displayed. Here the same in another zoom:
Unfortunately, these lines are also visible on a printout. I guess the problem may be with how the coordinates in the PDF get rounded when the vector graphics is rendered to bitmap for display or printing.
A possible solution would be to use segments which overlap with each other. This is acceptable only for solid colors; unfortunately, I would like to use transparent colors in the gradients as well.
What can I do to make my output in PDF better?
This seems to be an issue purely due to the renderer. E.g.:
I don't believe there's anything you can change about the PDF to fundamentally fix the issue. In my case, Adobe Acrobat looked good at any zoom level except at very high zoom (I had to go to 3200% zoom to see white lines).
Also, Chrome and Microsoft Edge seemed to work well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With