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)))
png('old-style.png')
plot(x, y, main='Test plot', pch=21, col='blue', bg='lightblue')
abline(lm(y ~ x), col='red', lwd=2)
dev.off()
I get a result like this:
Well, installing Cairo and plotting
install.packages(c("Cairo"), repos="http://cran.r-project.org" )
library(Cairo)
CairoPNG('new-style.png')
plot(x, y, main='Test plot', pch=21, col='blue', bg='lightblue')
abline(lm(y ~ x), col='red', lwd=2)
dev.off()
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…
–
25-41=-16


January 25th, 2009 at 1:22 pm
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?
January 25th, 2009 at 2:47 pm
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!
January 25th, 2009 at 2:50 pm
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…
January 25th, 2009 at 3:00 pm
Maybe it’s a firefox issue … I just tried looking at the post in Konqueror and there the plots look fine…
January 25th, 2009 at 11:02 pm
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.
January 26th, 2009 at 6:35 am
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!)