Antialias plotting in R using Cairo

In my recent post on spatial SQL queries, I included plots showing the results of my performace tests.  I wanted to show the results, and a graph is good for that, but I wasn’t completely happy with the graphics. Mainly because they lack antialiasing.

I plotted the graphs using R and refuse to believe that R can not do a better job at it.

So a bit of googling tells me that indeed R can.  If I use the Cairo package for plotting.

With the plain old plotting to png, using some random points for a plot:

x <- seq(-100,100,length.out=100)
y <- sapply(x, function (z) rnorm(1,z,abs(z)))

plot(x, y, main='Test plot', pch=21, col='blue', bg='lightblue')
abline(lm(y ~ x), col='red', lwd=2)

I get a result like this:

Pretty ugly, right?

Well, installing Cairo and plotting

install.packages(c("Cairo"), repos="" )
plot(x, y, main='Test plot', pch=21, col='blue', bg='lightblue')
abline(lm(y ~ x), col='red', lwd=2)

gives me this:

I was hoping for more of an improvement. It is a little better, but it still looks a bit ugly on the webpage.

The improvement when I look in an image viewer is great, but the rendering here isn’t improved much.

So I’m back to looking for ways to get better graphics on my blog…


Author: Thomas Mailund

My name is Thomas Mailund and I am a research associate professor at the Bioinformatics Research Center, Uni Aarhus. Before this I did a postdoc at the Dept of Statistics, Uni Oxford, and got my PhD from the Dept of Computer Science, Uni Aarhus.

6 thoughts on “Antialias plotting in R using Cairo”

  1. Ok, I don’t get this. If I click on the images and get them rendered without the surrounding html code, it looks perfect. But obviously it looks like crap otherwise. What is going on here?

  2. Isn’t it just that your HTML embedding also applies some scaling to the figure? As we all know, bitmap images are not very forgiving with regard to re-scaling. That is why we are using LCD panel at their native resolution, right?

    On another issue, I am not sure why everybody is so fond of antialiasing. In my non-expert view, the only thing anti-aliasing does is making things look blurry. I can get the same effect by taking off my glasses. No more stair effects!

  3. The HTML shouldn’t scale the images. Hacking the raw html in the post so it explicitly doesn’t, doesn’t seem to have any effect either.

    As for antialias vs blurry, I probably agree for text, but not for graphs. A straight line actually looks straight to me when antialiased but not otherwise… there it is more a concatenation of shorter line segments…

  4. Maybe it’s a firefox issue … I just tried looking at the post in Konqueror and there the plots look fine…

  5. Perhaps you’ve set Firefox to zoom in on your blog by default? I know I had, and your graphs looked bad, but after canceling zooming, they look absolutely fine, esp. the second one.

  6. Widmann, you lovely man, you are absolutely right :)

    Well, my first attempt at resetting zoom froze the box and forced me to reboot, but the second fixed the figures.

    I owe you a beer. You’ll get it next time you are in .dk or next time I’m in Scotland. (Invite me to your wedding and you are sure to get it :) congrats with that!)

Leave a Reply