9

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.

Community
  • 1
  • 1
Newbie_R
  • 655
  • 7
  • 22

5 Answers5

7

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 :)

ZNK
  • 2,196
  • 1
  • 19
  • 25
  • Went back and tested it against vertical and angled text. It seems that it also works with these. – ZNK Feb 03 '15 at 06:22
2

This is a failing in Cairo. Major, from my point of view.

The cairo SVG surface (i.e. the back-end in Cairo used to "draw" on SVG) simply does not support the "text" tag. It does not understand about strings at all. Instead, it places each character (glyph) individually. So any SVG generated with Cairo is not useful if you want to post-process contained text with a vector editor. :(

The only mention I found on the cairo list was this one:

http://lists.cairographics.org/archives/cairo/2011-February/021777.html

Elmar
  • 31
  • 3
2

The svglite package exports text on Linux as desired.

[EDIT] According to this thread, there is also a way to remove the squeezing of the edited text into the fixed box width. Just remove the textLength field from the object in the XML editor.

Cheers

mgrewe
  • 21
  • 2
1

Can't directly comment on mgrewe answer because of my low reputation but thank you for the solution.

Implemented the textLength edit into R:

svgitem<-readLines('file.svg')
svgitem<-gsub('textLength=','tL=',svgitem)
writeLines(svgitem,'without_textLength.svg')

Text-box seems to be no longer affected after edition in Inkscape using the without_textLength.svg file and keeps a trace of old textLength renamed 'tL'.

Thanks again mgrewe, I've lost so many hours reformating text in Inkscape before seeing your answer.

Vivi
  • 65
  • 7
0

R is clearly not using the standard SVG text objects for producing its labels. I have no idea why. I am not an R user.

Perhaps by default it uses it's own custom font that it manually inserts glyph-by-glyph into the output. Are you using the same font in both cases? In Inkscape you are using Palatino. Is that what you are using for the labels in R?

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • Tnx for the reply @BigBadaboom. It is not so important to use the same font *per se*, but rather to ensure that Inkscape has font family and type installed that match R output. Considering this issue, according to [support.rstudio.org](http://support.rstudio.org/help/discussions/questions/446-whats-the-name-of-the-default-font-type-in-r-studio) forum thread, Rstudio (which I am using) has default font **Lucida Console**. Inkscape has that font as well. Palatino was just an example in order to show how Inkscape treats text objects compared to the "user defined" objects from R graphs. – Newbie_R Aug 16 '13 at 09:06
  • Here are some examples that people handled text from R graphics in Inkscape as text objects: [1](http://rforwork.info/2012/04/12/fun-editing-r-graphs-in-inkscape/) [2](http://www.carlislerainey.com/2011/09/26/using-inkscape-to-post-edit-labels-in-r-graphs/) But, when I tried to do so with my data, or with the example posted above, I was not able to handle R "text" objects in Inkscape. – Newbie_R Aug 16 '13 at 09:12
  • I am stuck guessing at what might be going on. Are you able to update your question with a sample SVG file that is causing problems? – Paul LeBeau Aug 17 '13 at 06:53
  • @ BigBadaboom tnx for the suggestion. I added a _link_ of the svg example (plot.svg) to the qestion. Note that, the font used in the svg file is "Sans", even that the default font of Rstudio is "Lucida Console". – Newbie_R Aug 17 '13 at 14:37
  • 2
    For some reason R has included each character glyph as an SVG `` element. In other words it is treating each letter like it was a plain old graphic shape. I have no idea why they made that choice. There is no obvious reason for it. You might have to ask the R folks directly for answers. Anyway the upshot is that there is no way you will be able to edit the text in Inkscape or any other SVG editor. Your only choice is to manually replace the text yourself. Sorry. – Paul LeBeau Aug 17 '13 at 16:33