data.tree icon indicating copy to clipboard operation
data.tree copied to clipboard

Trouble saving plots under format other than html

Open paulegre opened this issue 8 years ago • 8 comments

Data.tree allows you to generate plots for trees very easily with the "plot" command. However, saving the resulting image in formats such as png, pdf, or jpeg fails (even using Rstudio). The issue was mentioned already on the web ( http://stackoverflow.com/questions/42108105/in-r-how-do-i-save-a-data-tree-plot-to-a-file) but the answers provided there did not work for me, so I have used screenshots for the time being. I will continue to look into the documentation from the inbuilt packages used by data.tree, but it would be nice eventually if some additional piece of code can be extracted for users of data.tree like me who appreciate the self-contained aspect of the package.

paulegre avatar Mar 18 '17 21:03 paulegre

The code there doesn't work with newer versions of data.tree (The grViz(ToGraphViz()) process has changed). This does work:

data(acme)
treeAsSVG <- export_svg(render_graph(ToDiagrammeRGraph(acme)))
writeLines(treeAsSVG, "filename.svg")

sebastian-c avatar Mar 23 '17 13:03 sebastian-c

Thx, Sebastian.

A similar way is this:

Pre-requisite: DiagrmmeRsvg and dependencies need to be installed

Depending on your OS, you might have to install V8. For example on ubuntu:

sudo apt-get install libv8-3.14-delibv8-3.14-dev
install.packages("DiagrammeRsvg")

On my Windows system, 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")

gluc avatar Mar 24 '17 05:03 gluc

Thank you Sebastian and Christoph. I am still stuck, though, probably on the step described by Christoph. When I execute the code, even with DiagramRsvg installed, I get an error message: "Error in loadNamespace(name) : there is no package called ‘rsvg’". I am using OS El Capitan 10.11.6 but no idea if that's relevant.

paulegre avatar Mar 25 '17 13:03 paulegre

Are you sure DiagrammeRsvg installed without errors? Can you check if the r package is installed? My guess is that v8 is not installed. Here's a link, though I couldn't try it out myself: http://macappstore.org/v8/ If that doesn't help, could you please send me the output of sessionInfo()

gluc avatar Mar 25 '17 14:03 gluc

Thanks Christoph. I could install v8 following the link you sent. But the problem persists. I will send you the output of my session, just based on trying to execute the code you sent on the acme data. Many thanks again.

paulegre avatar Mar 25 '17 14:03 paulegre

@paulegre This is likely due to an install issue with rsvg. On Mac you will need to install librsvg. See comments on jeroen's github page linked below

https://github.com/jeroen/rsvg

vnijs avatar Mar 25 '17 17:03 vnijs

Thanks much. Following your advice I installed librsvg from my Terminal, following instructions found at: http://macappstore.org/librsvg/ but that does not seem to fix the problem even after restarting my Mac. So the problem may come from my version of R, as asked by Christoph. I will try to update it and will keep you all posted. Thank you so much for your support.

paulegre avatar Mar 25 '17 20:03 paulegre

Hi Cristoph, following your instructions:

library(data.tree) data(acme) library(DiagrammeR) export_graph(ToDiagrammeRGraph(acme), "export.pdf")

I was wondering if there is a way to add a title and legend to the exported pdf file. Thanks!

NataGH avatar Mar 27 '17 07:03 NataGH