graph2vector: Save currently active R graph to vector format

View source: R/graph2vector.R

graph2vectorR Documentation

Save currently active R graph to vector format

Description

Save the currently active R graph or a graph passed as an object or function to vector format with sensible defaults

Usage

graph2vector(
  x = NULL,
  file = "Rplot",
  fun = NULL,
  type = "SVG",
  aspectr = NULL,
  width = NULL,
  height = NULL,
  scaling = 100,
  font = ifelse(Sys.info()["sysname"] == "Windows", "Arial", "Helvetica")[[1]],
  bg = "white",
  colormodel = "rgb",
  cairo = TRUE,
  fallback_resolution = 600,
  ...
)

graph2svg(...)

graph2pdf(...)

graph2eps(...)

Arguments

x

given ggplot2 plot or lattice plot object to export; if set to NULL the currently active R graph will be exported; not supported for base R plots.

file

name of output file. Any extension is ignored and added according to the requested output type. If file already exists it is overwritten.

fun

plot passed on as a function used to create it; useful especially for base R plots.

type

desired output type - SVG, PDF or EPS are currently supported. SVG is the preferred format, and good for editing in Inkscape; PDF is good for printing; EPS is sometimes requested by journals, though lower quality, especially when semi-transparency is used, as this is rasterized to bitmap. graph2office is recommended for vector output to Microsoft Office.

aspectr

desired width to height aspect ratio. If set to NULL, the aspect ratio of the graphics device is used. Can also be combined with one value for either the desired width or height of the graph.

width

desired width in inches; can be combined with a desired aspect ratio aspectr.

height

desired height in inches; can be combined with a desired aspect ratio aspectr.

scaling

scale width & height by a certain percentage.

font

desired font to use for labels; defaults to "Arial" on Windows systems and to "Helvetica" on other systems. Fonts are embedded by default in EPS output.

bg

desired background colour, e.g. "white" or "transparent".

colormodel

desired colormodel in pdf or eps output when cairo=FALSE; currently allowed values are "rgb" (default), "cmyk", "srgb", "srgb+gray", "rgb-nogray", and "gray" (or "grey").

cairo

logical indicating whether or not to use the cairo graphics device for output to PDF or EPS, defaults to TRUE, thereby allowing for simulated semi-transparency in EPS output, by rasterizing semi-transparent sections, and automated font embedding.

fallback_resolution

resolution in dpi to use to rasterize non-supported vector graphics (e.g. semi-transparent vector elements in EPS) output).

...

any other options are passed on to svg, cairo_pdf, cairo_ps, pdf or postscript.

Value

No return value

Functions

  • graph2svg(): Save currently active R graph to SVG format

  • graph2pdf(): Save currently active R graph to PDF format

  • graph2eps(): Save currently active R graph to EPS format

Author(s)

Tom Wenseleers

See Also

graph2office, graph2bitmap, graph2png, graph2tif, graph2jpg

Examples

# Create a file name
filen <- tempfile(pattern = "ggplot") # or 
# filen <- paste("YOUR_DIR/ggplot")

# Generate graphical output
library(ggplot2)
library(datasets)
x=qplot(Sepal.Length, Petal.Length, data = iris, 
        color = Species, size = Petal.Width, alpha = I(0.7))
plot.fun <- function(){
  print(qplot(Sepal.Length, Petal.Length, data = iris, 
              color = Species, size = Petal.Width, alpha = 0.7))
}

# There are 3 ways to use graph2vector():
### 1. Pass the plot as an object
graph2svg(x=x, file=filen, aspectr=2, font = "Times New Roman", 
          height = 5, bg = "white")
graph2pdf(x=x, file=filen, aspectr=2, font = "Arial",   
          height = 5, bg = "transparent")
graph2eps(x=x, file=filen, aspectr=2, font = "Arial",   
          height = 5, bg = "transparent")
### 2. Get the plot from current screen device
if (interactive()) { # Because the example uses screen devices 
  x
  graph2svg(file=filen, aspectr=2, font = "Arial",  
            height = 5, bg = "transparent")
  graph2pdf(file=filen, aspectr=2, font = "Times New Roman",   
            height = 5, bg = "white")
  graph2eps(file=filen, aspectr=2, font = "Times New Roman",   
            height = 5, bg = "white")
}
### 3. Pass the plot as a function
if (interactive()) { # Because the example uses screen devices 
  graph2svg(file=filen, fun = plot.fun, aspectr=2, font = "Arial",  
            height = 5, bg = "transparent")
  graph2pdf(file=filen, fun=plot.fun, aspectr=2, font = "Arial",   
            height = 5, bg = "transparent")
  graph2eps(file=filen, fun=plot.fun, aspectr=2, font = "Arial",   
            height = 5, bg = "transparent")
} 


export documentation built on Dec. 7, 2022, 5:13 p.m.