Learn R Programming

export (version 0.3.0)

graph2bitmap: Save currently active R graph to bitmap format

Description

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

Usage

graph2bitmap(
  x = NULL,
  file = "Rplot",
  fun = NULL,
  type = c("PNG", "JPG", "TIF"),
  aspectr = NULL,
  width = NULL,
  height = NULL,
  dpi = 300,
  scaling = 100,
  font = ifelse(Sys.info()["sysname"] == "Windows", "Arial", "Helvetica")[[1]],
  bg = "white",
  cairo = TRUE,
  tiffcompression = c("lzw", "rle", "jpeg", "zip", "lzw+p", "zip+p"),
  jpegquality = 99,
  ...
)

graph2png(...)

graph2tif(...)

graph2jpg(...)

Value

No return value

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 - PNG, TIF or JPG are currently supported. PNG is the preferred format, as it is a lossless format, and compresses better than TIF.

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.

dpi

desired output in dpi; defaults to 600 dpi.

scaling

scale width & height by a certain percentage.

font

desired font to use for labels in PNG and TIFF output; defaults to "Arial" on Windows systems and to "Helvetica" on other systems.

bg

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

cairo

logical, specifying whether or not to use Cairographics for export.

tiffcompression

compression to use for TIF files.

jpegquality

quality of JPEG compression.

...

any other options are passed on to grDevices' png, tiff, or jpeg function (according to the supplied type).

Functions

  • graph2png(): Save currently active R graph to png file

  • graph2tif(): Save currently active R graph to TIF file

  • graph2jpg(): Save currently active R graph to JPEG file

Author

Tom Wenseleers

See Also

graph2office, graph2vector, graph2svg, graph2pdf, graph2eps

Examples

Run this code
# 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 graph2bitmap():
### 1. Pass the plot as an object
graph2png(x = x, file = filen, dpi = 400, height = 5, aspectr = 4)
graph2tif(x = x, file = filen, dpi = 400, height = 5, aspectr = 4)
graph2jpg(x = x, file = filen, dpi = 400, height = 5, aspectr = 4)
### 2. Get the plot from current screen device
x
graph2png(file = filen, dpi = 400, height = 5, aspectr = 4)
graph2tif(file = filen, dpi = 400, height = 5, aspectr = 4)
graph2jpg(file = filen, dpi = 400, height = 5, aspectr = 4)
### 3. Pass the plot as a function
graph2png(file = filen, fun = plot.fun, dpi = 400, height = 5, aspectr = 4)
graph2tif(file = filen, fun = plot.fun, dpi = 400, height = 5, aspectr = 4)
graph2jpg(file = filen, fun = plot.fun, dpi = 400, height = 5, aspectr = 4)

Run the code above in your browser using DataLab