Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R & Inkscape: text labels in SVG graphics exported from R did not recognized as a text in Inkscape

I constructed dendrogram in R with the code:

data(iris)

aver<-sapply(iris[,-5],function(x) by(x,iris$Species,mean))
matrix<-dist(aver)

clust<-hclust((matrix),"ave")
clust$labels<-row.names(aver)
plot(as.dendrogram(clust))

I wanted to save the dendrogram as svg file using the code:

install.packages("Cairo")
library(Cairo)

svg("plot.svg")
plot(as.dendrogram(clust))
dev.off()

Here the problem started:

When I imported the "plot.svg" into Inkscape (ver: 0.48.4) and selected any label (e.g. "setosa") it was not recognized as a text, but rather as some "user defined" object. Specifically, when I selected any "letter" in the label and inspect it with the XML Editor (ctrl+shift+X) in Inkscape I obtained this information:

    **id**: use117

    **x**: 142.527344

    **xlink:href**: #glyph0-8

    **y**: 442.589844

On the other hand, when I manually wrote "setosa" using "create and edit text objects" tool, and inspected in XML Editor, it returned:

    **id**: text4274

    **sodipodi:linespacing**: 125%

    **style**: font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Palatino Linotype;-inkscape-font-specification:Palatino Linotype

     **transform**: scale(0.8,0.8)

     **x**: 176.02016

     **xml:space**: preserve

     **y**: 596.96674

It is likely that Inkscape did not recognize the labels as a text according to the attribute "id" from XML Editor. Hence, I am not able to change neither font, size as well as use other functions related to text objects in Inkscape.

Here is the svg file, that I made with the previous code and imported into Inkscape

I checked previous steps using other versions of Inkscape as well as R, but it would be the same.

Here is the question:

Do you have any suggestion how I can gather labels as a text attribute instead of a "user defined" (or whatever it is object...) when importing svg files from R into Inkscape?

UPDATE

@baptiste linked to the SO thread where @Oscar Perpiñán suggested three packages (gridSVG, SVGAnnotation and RSVGTipsDevice) that manipulate SVG. Unfortunately, neither of packages suggested could solve the problem with the text issue. So far I found SO thread where @Mo Sander suggested RSvgDevice package since it can preserve text object rather than glyphs. Being stuck with the RSvgDevice installation procedure, I found that it RSvgDevice is only available for 32-bit installations and R < 2.15.0. Otherwise, R returned warning message:

Warning message:
package ‘RSvgDevice’ is not available (for R version 3.0.1) 

Beside the requirements for older R versions, currently only RSvgDevice can preserve a text object in SVG.

like image 954
Newbie_R Avatar asked Aug 15 '13 17:08

Newbie_R


1 Answers

I'm a bit late to the party, but I've been dealing with this myself. I found a trick to make it work. First, I export the plot as PDF instead of SVG because PDF fonts are recognized by inkscape.

This, however brings a new problem as the text often ends up being defined letter by letter meaning that you can change the font, but the spacing is still defined and it becomes immensely annoying. I found that it was due to the x coordinate being defined at each letter.

I wrote a perl script and put it in this gist to remove all the trailing coordinates. After that I'm able to manipulate all the fonts I wished. Note, that this will only work for horizontal text.

Hope that helps this problem you had over a year ago :)

like image 187
ZNK Avatar answered Oct 03 '22 03:10

ZNK