[R] display SVG, PNG, GIF, JPEG, TIFF, PPM in new plot frame

Robbie Morrison robbie at actrix.co.nz
Mon Jun 15 21:42:15 CEST 2009


Dear R-help

I want to display an image file in a new plot frame.

SVG is my preferred format, but I can also consider
PNG, GIF, JPEG, TIFF, and PPM (from ImageMagick).

By way of background (although not material to this
posting), the image file is generated by a call to
'dot' (part of the 'Graphviz' suite) after the required
.dot file has been produced using hand-crafted R code.

I have reduced my options to the following.  Any
corrections, suggestions, or comments would be most
welcome.

I realize the list is quite long, but if the feedback
converges, I will refine the material below and add it
to a new page on the R-wiki.

    # HEATH WARNING: none of this code has been run
    # ---------------------------------

    x11()       # open another plot frame as required (UNIX-alikes)
    plot.new()

    # SVG (hypothetical code)
    # ---------------------------------
    # SVG is not supported in this context (it would
    # appear)

    library(?????)
    svg <- read.svg(file = "my.svg")  # hypothetical call
    plot(svg)

    # PNG, JPEG, GIF, TIFF, PPM
    # ---------------------------------
    # package 'EBImage' is an image processing toolkit
    # -- the package requires ImageMagick 6.2.4 or
    # better

    library(EBImage)
    g   <- system.file("images", "my.gif", package = "EBImage")
    gif <- readImage(g)
    display(gif)

    # similarly for "my.png", "my.jpg", "my.ppm", and
    # "my.tiff", depending on the capabilities of the
    # 'ImageMagick' installation you have

    # PNG, JPG, TIFF
    # ---------------------------------
    # package 'rgdal' -- requires system library and
    # headers for the 'GDAL' geospatial data library

    library(rgdal)
    p <- system.file("my.png", package = "rgdal")
    h <- GDAL.open(p)                # returns a file handle
    getDriverLongName(getDriver(h))  # should report "Portable Network Graphics"
    displayDataset(h)
    GDAL.close(h)

    # note that recent versions of 'ReadImages' (see
    # the JPEG section) will also read PNG [is the call
    # still 'read.jpg'? -- or is it now 'read.png'?]

    # JPEG
    # ---------------------------------
    # package 'rimage' provides functions for
    # processing images and for reading JPEG files --
    # this package requires system libraries and
    # headers for  'libjpeg' and 'fftw-2'
    #
    # package 'ReadImages' is essentially a subset of
    # 'rimage' and does not require 'fftw-2'

    library(ReadImages)  # subset of package 'rimage'
    j    <- system.file("data", "my.jpg", package = "ReadImages")
    jpeg <- read.jpeg(j)
    plot(jpeg)

    library(rimage)
    j    <-system.file("data", "my.jpg", package = "rimage")
    jpeg <- read.jpeg(j)
    plot(jpeg)

    # GIF
    # ---------------------------------
    # package 'caTools' provides a number of utility
    # functions, including calls for reading and
    # writing GIF files -- this package depends on
    # package 'bitopts' (but contains no system
    # requirements)
    #
    # note that image() needs 'flip' = TRUE and 'asp'
    # sets the aspect ratio to unity

    library(caTools)
    gif <- read.gif("my.gif", verbose = TRUE, flip = TRUE)
    image(gif$image, col = gif$col, main = gif$comment, asp = 1)

    # TIFF
    # ---------------------------------
    # package 'rtiff' reads TIFF format images and
    # return them as pixmaps

    library(rtiff)
    tif <- read.tiff("my.tif")
    plot(tif)

    # PPM (similarly for PGM and PPM)
    # ---------------------------------
    # package 'pixmap' provides functions for the
    # import, export, plotting and manipulation of
    # bitmapped images (the package has no R
    # dependencies or system requirements)

    library(pixmap)
    p   <- system.file("my.ppm", package="pixmap")
    ppm <- read.pnm(p)
    plot(ppm)

Additional material can be added to the plot frame,
thereby relegating the original graphic to that of a
backing image.

See also the following packages:

    'GDD' - produce bitmaps without X11

Finally, the ImageMagick 'display' utility can be
called directly from R, thereby bypassing the need to
read and display the graphics file as data:

    frameTitle      <- "\"override default\""
    graphic         <- "my.png"  # ImageMagick also supports PNG, GIF, JPEG, TIFF, PPM
    imagemagickCall <- paste("display", "-title", frameTitle, graphic, "&")
    ret             <- system(imagemagickCall)
    if ( ret != 0 ) warning("display call returned fail: ", ret)

thanks in advance, Robbie
---
Robbie Morrison
PhD student -- policy-oriented energy system simulation
Technical University of Berlin (TU-Berlin), Germany
University email (redirected) : morrison at iet.tu-berlin.de
Webmail (preferred)           : robbie at actrix.co.nz
[from IMAP client]




More information about the R-help mailing list