<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: Tag graphics</title>
    <link>http://blog.moertel.com/articles/tag/graphics?tag=graphics</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title> R tips and tricks:  Producing smooth bitmap plots</title>
      <description>&lt;p&gt;The &lt;a href="http://www.r-project.org/"&gt;R statistics system&lt;/a&gt; can produce
first-class data visualizations, commonly known as plots.  Internally,
plots are represented in an abstract graphics format that can be
rendered on any of R&amp;#8217;s wide range of graphics &amp;#8220;devices&amp;#8221; to produce
concrete output &amp;#8211; windows, bitmap files, PostScript files, &lt;span class="caps"&gt;PDF&lt;/span&gt; files,
and others.&lt;/p&gt;


	&lt;p&gt;The bitmap formats, such as &lt;span class="caps"&gt;PNG&lt;/span&gt;, are preferred for posting
plots online because of their widespread support by web browsers.  The
default bitmap-rendering devices in R, unfortunately, produce graphics
that look a little too &amp;#8220;bitmapped&amp;#8221; for modern web tastes.  Here, for example,
is a plot rendered by R&amp;#8217;s &amp;#8220;png&amp;#8221; device:&lt;/p&gt;


&lt;div class="photo"&gt;

	&lt;p&gt;&lt;img src="http://community.moertel.com/~thor/blog/pix-20070825/plot.png" title="Plot rendered via R's PNG device" alt="Plot rendered via R's PNG device" /&gt;&lt;/p&gt;


&lt;/div&gt;

	&lt;p&gt;There&amp;#8217;s nothing technically wrong with the plot, but it looks out
of place on a web page.  That&amp;#8217;s because modern web
browsers use font-smoothing and anti-aliasing techniques to render
just about everything else on the page.  Against this clean, un-jagged
backdrop, the oh-so-bitmapped plot looks like a throwback to
a previous era.&lt;/p&gt;


	&lt;p&gt;Happily, we &lt;em&gt;can&lt;/em&gt; produce clean, anti-aliased R plots with a little
help.  Here&amp;#8217;s the earlier plot, anti-aliased:&lt;/p&gt;


&lt;div class="photo"&gt;

	&lt;p&gt;&lt;img src="http://community.moertel.com/~thor/blog/pix-20070825/plot2.png" title="Plot rendered via R's PDF device, then post-processed" alt="Plot rendered via R's PDF device, then post-processed" /&gt;&lt;/p&gt;


&lt;/div&gt;

	&lt;p&gt;To produce the anti-aliased plot, I used R to produce a &lt;span class="caps"&gt;PDF&lt;/span&gt; file.  Then I
rendered the &lt;span class="caps"&gt;PDF&lt;/span&gt; file into a &lt;span class="caps"&gt;PNG&lt;/span&gt; image at 300 dpi using Ghostscript.
Finally, I scaled the 300-dpi image down to screen resolution,
producing a high-quality, anti-aliased result.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s the recipe in detail.&lt;/p&gt;


	&lt;p&gt;First, I define an R function called &lt;em&gt;pdfit&lt;/em&gt; that takes an
abstract graphics object and makes a &lt;span class="caps"&gt;PDF&lt;/span&gt;-file rendering of it, using
my preferred graphics-device settings:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;require("lattice")

pdfit &amp;lt;- function(f, ...) {
  trellis.device(dev=pdf, theme="col.whitebg", ...);
  print(f);
  dev.off()
}
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Then, when I create a plot I want to publish, I use &lt;em&gt;pdfit&lt;/em&gt; to render
it into a &lt;span class="caps"&gt;PDF&lt;/span&gt; file:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;P.img &amp;lt;- xyplot( subs.low + subs.high ~ date, ... )

pdfit(P.img, file="image-downloads.pdf")  # render plot into PDF file
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Finally, I use &lt;a href="http://www.ghostscript.com/"&gt;Ghostscript&lt;/a&gt; and
&lt;a href="http://www.imagemagick.org/"&gt;ImageMagick&lt;/a&gt; to convert the &lt;span class="caps"&gt;PDF&lt;/span&gt; file into
a high-quality, anti-aliased &lt;span class="caps"&gt;PNG&lt;/span&gt; file.  (I keep both formats: the &lt;span class="caps"&gt;PDF&lt;/span&gt;
file is best for publishing in printed papers, and the &lt;span class="caps"&gt;PNG&lt;/span&gt; file is
best for posting online.)  I use a simple Makefile to automate the
process of converting the &lt;span class="caps"&gt;PDF&lt;/span&gt; files into &lt;span class="caps"&gt;PNG&lt;/span&gt; files:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# Makefile (GNU make)

pdfs := $(wildcard *.pdf)
pngs := $(pdfs:.pdf=.png)

all: $(pngs)
.PHONY: all

%.png: %.pdf
    gs -dSAFTER -dBATCH -dNOPAUSE -sDEVICE=png16m \
       -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -r300 \
       -dBackgroundColor='16#ffffff' \
       -sOutputFile=$@ &amp;gt; /dev/null \
       $&amp;lt; &amp;#38;&amp;#38; \
    mogrify -resize 500 $@
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;With this Makefile in my graphics directory, just a single &amp;#8220;make&amp;#8221; 
command is all it takes to convert my &lt;span class="caps"&gt;PDF&lt;/span&gt; images into
anti-aliased &lt;span class="caps"&gt;PNG&lt;/span&gt; files, ready to post online.&lt;/p&gt;


	&lt;p&gt;And that&amp;#8217;s it.&lt;/p&gt;


	&lt;p&gt;Do you have any tips or tricks for making good-looking graphics with
R?  If so, please do share.&lt;/p&gt;


&lt;div class="update"&gt;

	&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;  There is one downside to the sexy, anti-aliased plots: they
are not as compressible as the old-style jagged plots.  For the
images above, for instance, the anti-aliased &lt;span class="caps"&gt;PNG&lt;/span&gt; file weighs in
at 45&amp;#160;KB, but the original &lt;span class="caps"&gt;PNG&lt;/span&gt; file is a feathery 4.7&amp;#160;KB.
So, if bandwidth is precious to you &amp;#8211; or you&amp;#8217;re planning on getting
Slashdotted &amp;#8211; you might want to stick
with the jaggies.&lt;/p&gt;


&lt;/div&gt;</description>
      <pubDate>Sat, 25 Aug 2007 21:56:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:c0e03deb-df96-4c4b-aea7-a4d9a256421b</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2007/08/25/r-tips-and-tricks-producing-smooth-bitmap-plots</link>
      <category>statistics</category>
      <category>R</category>
      <category>statistics</category>
      <category>plots</category>
      <category>graphics</category>
      <category>tips</category>
      <category>tricks</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/550</trackback:ping>
    </item>
  </channel>
</rss>
