[R] [TIP] Apropos resolution of plots

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jun 18 00:39:11 CEST 2004


If you need to make JPEG plots for the web or suchlike, here's a
method for "poor mans antialiasing" that seems to turn out rather
nice:

[Requires ghostscript, netpbm]

In R (lifted out of example(contour))

bitmap("out.ppm","ppmraw",res=4*72, pointsize=12)
     data("volcano")
     rx <- range(x <- 10*1:nrow(volcano))
     ry <- range(y <- 10*1:ncol(volcano))
     ry <- ry + c(-1,1) * (diff(rx) - diff(ry))/2
     tcol <- terrain.colors(12)
     par(pty = "s", bg = "lightcyan")
     plot(x = 0, y = 0,type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
     u <- par("usr")
     rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
     contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
             vfont = c("sans serif", "plain"))
     title("A Topographic Map of Maunga Whau", font = 4)
     abline(h = 200*0:4, v = 200*0:4, col = "white", lty = 2, lwd= 0.2)
dev.off()

and then from the command line

  pnmsmooth -size 5 5 out.ppm > smooth.ppm
  pnmscale .25 smooth.ppm > aa.ppm
  pnmtojpeg -quality 100 aa.ppm > aa.jpg

Notice that the intermediate files tend to get rather large even
though the end result is quite compact, so don't forget to clean up:

$ ls -l *.ppm *.jpg
-rw-rw-r--    1 pd       pd          75393 Jun 18 00:34 aa.jpg
-rw-rw-r--    1 pd       pd         559887 Jun 18 00:34 aa.ppm
-rw-rw-r--    1 pd       pd        8958022 Jun 18 00:34 out.ppm
-rw-rw-r--    1 pd       pd        8957969 Jun 18 00:34 smooth.ppm

You could in principle use a lower -quality in the final jpeg
conversion, but at least to my eyes some unsightly artifacts creep in
rather quickly.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list