Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdf of figure in R has unsightly white lines in it

Tags:

r

graphics

I'm trying to make colorbars, as well as raster maps, in R, and the output figures give unsightly lines in them when exported to pdf.

Here is code to generate a colorbar. It looks fine when you run it in R:

color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
    scale = (length(lut)-1)/(max-min)

    plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
    axis(4, ticks, las=1)
    for (i in 1:(length(lut)-1)) {
     y = (i-1)/scale + min
     rect(0,y,10,y+1/scale, col=lut[i], border=NA)
    }
}

par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = colorRampPalette(c("yellow","darkgreen"))
pos = pal(50)
color.bar(c(neg,pos),min=-75,max=50,ticks=c(-75,-50,-25,0,25,50))
color.bar(colorRampPalette(c("goldenrod","blue"))(25),min=0,max=1)
par(mar=c(5.1,4.1,4.1,2.1))
    dev.copy2pdf(file = "colorbar_wood.pdf", height = 8, width = 1)
pdf("colorbar_wood.pdf",width=1,height=8)
par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = colorRampPalette(c("yellow","darkgreen"))
pos = pal(50)
color.bar(c(neg,pos),min=-75,max=50,ticks=c(-75,-50,-25,0,25,50))
color.bar(colorRampPalette(c("goldenrod","blue"))(25),min=0,max=1)
par(mar=c(5.1,4.1,4.1,2.1))
 dev.off()

And here is what I get out as a pdf:

link

I need to get this up to publication quality. Any ideas on how to fix?

like image 901
generic_user Avatar asked Mar 26 '13 02:03

generic_user


1 Answers

This is invariably an issue with the software used to render the PDF, not with R, and arises because of features such as anti-aliasing and other rendering operations that the PDF Viewer does in order to display the PDF.

This is discussed in ?pdf, notably

Note:

     If you see problems with PDF output, do remember that the problem
     is much more likely to be in your viewer than in R.  Try another
     viewer if possible.  Symptoms for which the viewer has been at
     fault are apparent grids on image plots (turn off graphics
     anti-aliasing in your viewer if you can) and missing or incorrect
     glyphs in text (viewers silently doing font substitution).

     Unfortunately the default viewers on most Linux and Mac OS X
     systems have these problems, and no obvious way to turn off
     graphics anti-aliasing.

     ....

I just viewed your PDF in two different PDF viewers on Linux (Evince and Okular) and the degree to which these artefacts affected the file was different across the two viewers, with Okular giving fewer artefacts on the red-green one and none on the blue-yellow one. As such this seems to be an issue with viewing the PDF and not something with R. Your figure should therefore be publication quality.

like image 94
Gavin Simpson Avatar answered Oct 14 '22 15:10

Gavin Simpson