Re: [R] latex, eps graphics and transparent colors / sam2p
On 04/13/2011 05:06 PM, Ben Bolker wrote: Thomas Lumleytlumleyat uw.edu writes: On Thu, Apr 14, 2011 at 5:30 AM, Michael Friendlyfriendlyat yorku.ca wrote: I have a diagram to be included in latex, where all my figures are .eps graphics (so pdflatex is not an option) You could use the pdf() device and then use pdf2ps to convert to PostScript. Clever. [snip] There's now an adjustcolor() function in base R to do this. That makes my solution more or less obsolete. This is a follow-up to this thread, for which I thank everyone who replied. I could have fiddled with adjustcolor() to avoid using transparent colors, but instead did some tests on generating .png or .pdf files with transparent colors and then converting to .eps. I tried the following, in various combinations [running on ubuntu 10.02 linux] input file: foo.{pdf,png} converters: pdf2ps, pdftopdf, convert (ImageMagick) All of these gave really *huge* output files, by a factor of 10-20 times the original. Finally, I remembered (the badly named) sam2p utility, http://pts.szit.bme.hu/sam2p/ http://code.google.com/p/sam2p/ I'm not sure what magic it uses for compression, but the results are quite impressive compared with the competition. Bottom line: the combination of .pdf + sam2p seems to work best in my tests, below. The result is even smaller than the input file, and I can't tell the difference in the onscreen display. % ls -l foo.* -rw-r--r-- 1 friendly staff 11794 2011-04-29 08:31 foo.pdf -rw-r--r-- 1 friendly staff 20775 2011-04-29 08:32 foo.png euclid: /tmp % pdf2ps foo.pdf foo-pdf2ps.eps euclid: /tmp % pdftops foo.pdf foo-pdftops.eps euclid: /tmp % convert foo.pdf foo-convert-pdf.eps euclid: /tmp % convert foo.png foo-convert-png.eps euclid: /tmp % sam2p foo.png foo-sam2p-png.eps This is sam2p v0.47-1. Available Loaders: PS PDF JAI PNG JPEG TIFF PNM BMP GIF LBM XPM PCX TGA. Available Appliers: XWD Meta Empty BMP PNG TIFF6 TIFF6-JAI JPEG-JAI JPEG PNM GIF89a+LZW XPM PSL1C PSL23+PDF PSL2+PDF-JAI P-TrOpBb. sam2p: Notice: PNM: loaded alpha, but no transparent pixels sam2p: Notice: job: read InputFile: foo.png sam2p: Notice: writeTTT: using template: l23 sam2p: Notice: applyProfile: applied OutputRule #37 sam2p: Notice: job: written OutputFile: foo-sam2p-png.eps Success. sam2p foo.pdf foo-sam2p-pdf.eps This is sam2p v0.47-1. Available Loaders: PS PDF JAI PNG JPEG TIFF PNM BMP GIF LBM XPM PCX TGA. Available Appliers: XWD Meta Empty BMP PNG TIFF6 TIFF6-JAI JPEG-JAI JPEG PNM GIF89a+LZW XPM PSL1C PSL23+PDF PSL2+PDF-JAI P-TrOpBb. gs_cmd=(gs -r72 -q -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dLastPage=1 -sDEVICE=pnmraw -dDELAYSAFER -dBATCH -dNOPAUSE -sOutputFile=%D -- %S) sam2p: Notice: job: read InputFile: foo.pdf sam2p: Notice: writeTTT: using template: l23 sam2p: Notice: applyProfile: applied OutputRule #27 sam2p: Notice: job: written OutputFile: foo-sam2p-pdf.eps Success. euclid: /tmp % ls -l foo* -rw-r--r-- 1 friendly staff 1139852 2011-05-03 08:41 foo-convert-pdf.eps -rw-r--r-- 1 friendly staff 2924149 2011-05-03 08:41 foo-convert-png.eps -rw-r--r-- 1 friendly staff 11794 2011-04-29 08:31 foo.pdf -rw-r--r-- 1 friendly staff 1687255 2011-05-03 08:41 foo-pdf2ps.eps -rw-r--r-- 1 friendly staff 2775130 2011-05-03 08:41 foo-pdftops.eps -rw-r--r-- 1 friendly staff 20775 2011-04-29 08:32 foo.png -rw-r--r-- 1 friendly staff8701 2011-05-03 08:44 foo-sam2p-pdf.eps -rw-r--r-- 1 friendly staff 35926 2011-05-03 08:42 foo-sam2p-png.eps euclid: /tmp % __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] latex, eps graphics and transparent colors
On 04/14/2011 03:30 AM, Michael Friendly wrote: I have a diagram to be included in latex, where all my figures are .eps graphics (so pdflatex is not an option) and I want to achieve something like the following: three concentric filled circles varying in lightness or saturation. It is easiest to do this using transparency, but in my test using the postscript driver, the transparent color fills do not appear. Is it correct that postscript() does not support transparency? circle -function (radius = 1, segments=61) { angles - (0:segments)*2*pi/segments radius * cbind( cos(angles), sin(angles)) } plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='', asp=1, xaxt=n, yaxt=n) #clrs - trans.colors(lightblue, alpha=c(.2, .4, .6)) ## from heplots package clrs - c(#ADD8E633, #ADD8E666, #ADD8E699) c1 - circle(3) polygon( c1, col=clrs[1], border=lightblue) polygon(.67*c1, col=clrs[2], border=lightblue) polygon(.33*c1, col=clrs[3], border=lightblue) arrows(-1, 0, 5, 0, angle=10, length=.2, lwd=2, col=darkgray) arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col=darkgray) One alternative that sort of works is to use the png() driver, and then convert fig.png fig.eps but I need very high resolution to make the real diagram legible. It might suffice to use hcl() colors to approximate what I've done with transparency, but I don't know how to start with a given color (lightblue) and achieve roughly similar resuts. Hi Michael, For a one off job, I would probably do the transparent overlays in the pdf device, open the result in xpdf or Acrobat and then use the GIMP to identify the resulting colors from a screen shot. Then redo the plot in the postscript device using the opaque colors. This is only an approximation, of course, but it gets the job done. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] latex, eps graphics and transparent colors
Michael Friendly friendly at yorku.ca writes: I have a diagram to be included in latex, where all my figures are .eps graphics (so pdflatex is not an option) and I want to achieve something like the following: three concentric filled circles varying in lightness or saturation. It is easiest to do this using transparency, but in my test using the postscript driver, the transparent color fills do not appear. Is it correct that postscript() does not support transparency? circle -function (radius = 1, segments=61) { angles - (0:segments)*2*pi/segments radius * cbind( cos(angles), sin(angles)) } plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='', asp=1, xaxt=n, yaxt=n) #clrs - trans.colors(lightblue, alpha=c(.2, .4, .6)) ## from heplots package clrs - c(#ADD8E633, #ADD8E666, #ADD8E699) c1 - circle(3) polygon(c1, col=clrs[1], border=lightblue) polygon(.67*c1, col=clrs[2], border=lightblue) polygon(.33*c1, col=clrs[3], border=lightblue) arrows(-1, 0, 5, 0, angle=10, length=.2, lwd=2, col=darkgray) arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col=darkgray) One alternative that sort of works is to use the png() driver, and then convert fig.png fig.eps but I need very high resolution to make the real diagram legible. It might suffice to use hcl() colors to approximate what I've done with transparency, but I don't know how to start with a given color (lightblue) and achieve roughly similar resuts. If you really only want to lighten a specified colour (rather than overlaying multiple colours), then something like this ought to do the trick: testfun - function(clrs=c(#ADD8E633, #ADD8E666, #ADD8E699)) { plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='', asp=1, xaxt=n, yaxt=n) c1 - circle(3) polygon(c1, col=clrs[1], border=lightblue) polygon(.67*c1, col=clrs[2], border=lightblue) polygon(.33*c1, col=clrs[3], border=lightblue) arrows(-1, 0, 5, 0, angle=10, length=.2, lwd=2, col=darkgray) arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col=darkgray) } postscript(testalpha1.ps) testfun() dev.off() lblue - #ADD8E6 alphafy - function(col,alpha=1) { rr - 1-alpha*(1-c(col2rgb(col)/255)) rgb(rr[1],rr[2],rr[3]) } alphafy(#ADD8E6) alphafy(#ADD8E6,alpha=0) postscript(testalpha2.ps) testfun(clrs=c(alphafy(lblue,0.2),alphafy(lblue,0.4),alphafy(lblue,0.6))) dev.off() __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] latex, eps graphics and transparent colors
On Thu, Apr 14, 2011 at 5:30 AM, Michael Friendly frien...@yorku.ca wrote: I have a diagram to be included in latex, where all my figures are .eps graphics (so pdflatex is not an option) You could use the pdf() device and then use pdf2ps to convert to PostScript. and I want to achieve something like the following: three concentric filled circles varying in lightness or saturation. It is easiest to do this using transparency, but in my test using the postscript driver, the transparent color fills do not appear. Is it correct that postscript() does not support transparency? circle -function (radius = 1, segments=61) { angles - (0:segments)*2*pi/segments radius * cbind( cos(angles), sin(angles)) } plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='', asp=1, xaxt=n, yaxt=n) #clrs - trans.colors(lightblue, alpha=c(.2, .4, .6)) ## from heplots There's now an adjustcolor() function in base R to do this. package clrs - c(#ADD8E633, #ADD8E666, #ADD8E699) c1 - circle(3) polygon( c1, col=clrs[1], border=lightblue) polygon(.67*c1, col=clrs[2], border=lightblue) polygon(.33*c1, col=clrs[3], border=lightblue) arrows(-1, 0, 5, 0, angle=10, length=.2, lwd=2, col=darkgray) arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col=darkgray) One alternative that sort of works is to use the png() driver, and then convert fig.png fig.eps but I need very high resolution to make the real diagram legible. It might suffice to use hcl() colors to approximate what I've done with transparency, but I don't know how to start with a given color (lightblue) and achieve roughly similar resuts. It would be useful to have an alpha-blending function for this sort of purpose, but I don't think we have one. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] latex, eps graphics and transparent colors
Thomas Lumley tlumley at uw.edu writes: On Thu, Apr 14, 2011 at 5:30 AM, Michael Friendly friendly at yorku.ca wrote: I have a diagram to be included in latex, where all my figures are .eps graphics (so pdflatex is not an option) You could use the pdf() device and then use pdf2ps to convert to PostScript. Clever. [snip] There's now an adjustcolor() function in base R to do this. That makes my solution more or less obsolete. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.