[R] [TIP] Apropos resolution of plots

About this list Date view Thread view Subject view Author view Attachment view

From: Peter Dalgaard (p.dalgaard@biostat.ku.dk)
Date: Fri 18 Jun 2004 - 08:39:11 EST


Message-id: <x21xkdu7w0.fsf@biostat.ku.dk>


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@biostat.ku.dk)             FAX: (+45) 35327907

______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Wed 30 Jun 2004 - 23:05:08 EST