Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how do I save a data.tree plot to a file?

Tags:

plot

r

image

tree

save

I'm unable to save a plot generated by the plot.Node function in data.tree. I've tried the following:

### Create tree object and plot it
data(acme);
plot(acme);

This works fine, showing the plot, as one would expect.

### Try saving it as png
png(filename='file.png', type='cairo-png');
plot(acme);
dev.off();

This creates an empty file. ggsave does the same. Apparantly, plot.Node uses DiagrammeR under the hood, so I looked into that package. It has a function to export graphs:

export_graph(acme, file_name="file.png");

This gives the error:

Error in file.exists(diagram) : invalid 'file' argument

When I transform to GraphViz first, I get a different error:

export_graph(ToGraphViz(acme), file_name="file.png");

Error in graph$dot_code : $ operator is invalid for atomic vectors

Clearly, exporting to GraphViz doesn't quite export to what DiagrammeR expects.

I'm in RStudio and so could in theory just save the plot using the GUI, but I need this for in a script.

Apparently, plot.Node doesn't actually plot anything - instead it seems to generate html/js. Does this mean that that result cannot be stored as a graphic? Or is there some export/conversion function somewhere that I'm completely missing? it certainly feels like I'm missing something obvious - I assume the need to store plotted data.trees as images is quite common. But I have no idea which potential solutions I can still explore.

I'd be very grateful for any pointers anybody has!

like image 944
Matherion Avatar asked Feb 08 '17 08:02

Matherion


2 Answers

As sebastian-c suggested, things work now a bit differently than suggested by Matherion, as of R 3.3.3 with data.tree 0.7.0 and DiagrammeR 0.9.0

Pre-requisite: DiagrmmeRsvg and dependencies need to be installed. Depending on your OS, for this to work you might have to install V8. For example on ubuntu:

apt-get install libv8-3.14-delibv8-3.14-dev

And then in R:

install.packages("DiagrammeRsvg")

On Windows, I didn't have to install anything (maybe because Chrome is installed?).

Once DiagrammeRsvg is available, run:

library(data.tree)
data(acme)
library(DiagrammeR)
export_graph(ToDiagrammeRGraph(acme), "export.pdf")
like image 101
Christoph Glur Avatar answered Sep 28 '22 09:09

Christoph Glur


I've found the answer, at least, partially. There exists a package dedicated to exporting GraphViz diagrams to SVG: DiagrammeRsvg.

So this works:

treeAsSVG <- export_svg(grViz(ToGraphViz(acme)));
writeLines(treeAsSVG, "filename.svg"));

The grViz is necessary to actually convert the ToGRaphViz output to something that can be interpreted by export_svg. I'm still not really sure (yet) what all goes on under the hood - for example, this does not work:

export_graph(grViz(ToGraphViz(acme)), file_name="filename.svg");

But, if anybody else has a similar problem and stumbles upon this question, perhaps this partial answer can help them to at least export something that can then be integrated in e.g. html pages.

like image 26
Matherion Avatar answered Sep 28 '22 10:09

Matherion