Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color gradients in R in PDF and bitmap output

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:

screenshot 1

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:

screenshot 2

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?

like image 235
January Avatar asked Feb 22 '17 09:02

January


1 Answers

This seems to be an issue purely due to the renderer. E.g.:

enter image description here

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.

like image 193
thc Avatar answered Sep 30 '22 09:09

thc