Thursday, 21 July 2011

Importing pdf R plots into Inkscape: the "q" problem

I know some people like to do absolutely everything in R (it's like an admission of failure if they can't), but my approach is somewhat more pragmatic; I like to get things done as quickly and as easily as possible.

As is my usual way, I get most of my plot basics done in R, then touch them up and move things around afterwards in Inkscape. This really does make life simple, but recently I came unstuck with this method.

R was exporting my plot just fine, and the pdf looked okay, but when I imported it into Inkscape, the points had all turned into the letter "q". I think this a a fairly well known problem, and is caused by R using letters (i.e. "o") as circles in the plot, but Inkscape not being able to deal with the same fonts.


There are multitude of fixes out there on the Web, but here is what worked for me, and took about 30 seconds. Instead of using pdf as output, try postscript. The code is as follows:

postscript(file="plot.eps")
plot(object)
dev.off()


Inkscape can now import the eps file, and the points are nicely rendered as circles. I would be interested to know if this works across other platforms using other font packages ...

6 comments:

  1. I ended up using dev.copy2eps() because I like the ease of specifying the width, but thanks for the explanation

    ReplyDelete
  2. Even better. Apparently the issue is that the pdf function uses the dingbats font for its glyphs.

    pdf("plot.pdf",useDingbats=F)
    print(object)
    dev.off()

    ReplyDelete
  3. Thanks Aftersox. That works perfectly.

    dat <- sample(1:1000,100)
    pdf("plot.pdf",useDingbats=F)
    plot(dat)
    dev.off()

    ReplyDelete
  4. Thank you all! It works perfectly in window.
    dat <- sample(1:1000,100)
    pdf("plot.pdf",useDingbats=F)
    plot(dat)
    dev.off()

    ReplyDelete
  5. Thanks, this was very useful! Thank you Aftersox!

    ReplyDelete