Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting anti-aliased plots with R on Ubuntu

I upgraded my system and reinstalled R, and now my standard X-11 plots are not anti-aliased -- they look jagged and the font looks bad.

I seem to recall a problem like this in the past but don't remember what I did about it.

Some other info:

  • qplot also comes out non-anti-aliased
  • plotting to a png device produces non-anti-aliased output as well
  • plotting to a pdf device, however, produces nice looking anti-aliased output

Another thing: I've been running this version of R/Ubuntu for a while now, a couple months or so. I don't know if this plotting problem started immediately with the new R install, or if I did something after that to break it. I don't remember noticing the lack anti-aliasing before, but I may not have been paying attention or doing a lot of plotting.

Anyone know what the fix is? Currently I am running R 3.2.1, compiled from the source, with Ubuntu 14.04.3 LTS.

A few more things. Following the discussion here I tried installing Cairo, but it failed. Also, I've been able to get non-anti-aliased plots in R/linux without installing Cairo in the past, and I'd rather not install extra things if not necessary.

Here are my X11.options():

$display
[1] ""

$width
[1] NA

$height
[1] NA

$pointsize
[1] 12

$bg
[1] "transparent"

$canvas
[1] "white"

$gamma
[1] 1

$colortype
[1] "true"

$maxcubesize
[1] 256

$fonts
[1] "-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"
[2] "-adobe-symbol-medium-r-*-*-%d-*-*-*-*-*-*-*"

$family
[1] "sans"

$xpos
[1] NA

$ypos
[1] NA

$title
[1] ""

$type
[1] "Xlib"

$antialias
[1] "default"
like image 783
mrip Avatar asked Sep 22 '15 13:09

mrip


1 Answers

I'm running R 3.4.0 and I get anti-aliased lines, "points", and plot axes by default for X11() and png() devices.

However, there are certain "Microsoft fonts" packages which needs to be installed on my system in order to get anti-aliased text. I can't speak for Ubuntu, but on Arch Linux the package names were "ttf-ms-fonts" and "fontconfig-ttf-ms-fonts", both in AUR. A good Google search should turn up similar packages for your own system.

Here is are some plots produced by the png() device with and without "ttf-ms-fonts" installed.

  • Without "ttf-ms-fonts":

brownian-motion-no-msfonts

  • With "ttf-ms-fonts":

brownian-motion-with-msfonts

I hope it is possible to see that the lines and circles in both plots are anti-aliased, but only the second plot has anti-aliased text.

Here is the code I used to produce the above plots:

set.seed(1);
brownian=cumsum(runif(1e3,min=-1));
png("brownian-no-msfonts.png",height=400);
par(cex=1.3);
plot(brownian,ylim=c(-10,15),
    ylab="Position",xlab="Time",main="Brownian Motion");
lines(brownian+7);
dev.off()

I've confirmed that I need both "ttf-ms-fonts" and "fontconfig-ttf-ms-fonts" (the latter I think configures certain fonts to be used by default) in order to get the anti-aliased text in R, although only the first package is needed to e.g. get anti-aliased text in Firefox.

However, I've been playing with the "knitr" package and I noticed that if I compile my documents using "render" from the "rmarkdown" package, then it can produce anti-aliased plots with or without the "ttf-ms-fonts" package installed. I haven't figured out how it does this. I know that it runs Pandoc which produces HTML with embedded fonts, but I'm not sure if the "rmarkdown" package itself includes fonts, or if it just has a better idea of where to find good ones on my system.

I feel superficial for spending time on this, but whatever.

like image 74
Metamorphic Avatar answered Oct 03 '22 06:10

Metamorphic