rich-iannone / DiagrammeR

Graph and network visualization using tabular data in R
https://rich-iannone.github.io/DiagrammeR/
Other
1.7k stars 249 forks source link

Save grviz to pdf, png, or similar #330

Open adeldaoud opened 5 years ago

adeldaoud commented 5 years ago

I would like to save grviz objects to to pdf, png, or similar formats. However, I have searched the web and DiagrammeR's issues (e.g. #66 , #70 ) but could not judge whether there is an implementation ready.

Tried a version of the DiagrammerRsvg package code (#133 ) that worked, but it does not seem to be generalized and natively packaged into DiagrammeR to support export of grViz. Are there plans for this?

thanks

jsavinc commented 4 years ago

FWIW, the way I do this at the moment is as follows:

library(tidyverse)  # for %>% pipes
library(DiagrammeR)
library(DiagrammeRsvg)  # for conversion to svg
library(rsvg)  # for saving svg

grViz_object %>%
    export_svg() %>%
    charToRaw %>% 
    rsvg_pdf("./path_to_svg_file.svg")
tbates commented 3 years ago

Just for those of us in the "Ceci n'est pas une pipe' crew:

# 1. Make a play graph
tmp = DiagrammeR::grViz('digraph{a->b; c->a; c->b; c->d;}')

# 2. Convert to SVG, then save as png
tmp = DiagrammeRsvg::export_svg(tmp)
tmp = charToRaw(tmp) # flatten
rsvg::rsvg_png(tmp, "g.png") # saved graph as png in current working directory
gasparsuliansky commented 1 year ago

Just for those of us in the "Ceci n'est pas une pipe' crew:

# 1. Make a play graph
tmp = DiagrammeR::grViz('digraph{a->b; c->a; c->b; c->d;}')

# 2. Convert to SVG, then save as png
tmp = DiagrammeRsvg::export_svg(tmp)
tmp = charToRaw(tmp) # flatten
rsvg::rsvg_png(tmp, "g.png") # saved graph as png in current working directory

does anybody know why might this be affecting the final graph output? precisely, I have text in my graph in "Times New Roman" font, which is correctly displayed in R own viewer, but the .png file after I save it displays another font

jrgant commented 1 year ago

@gasparsuliansky You may need to load the fonts into your graphic devices. Check out the extrafont package for more.