Time for action - exporting graphics

Now that we have created all of these informative graphics, it would be nice to be able to use them for presentations, reports, desktop wallpapers, or a variety of other purposes. Fortunately, R is capable of turning its graphics into digital images that can be used in other applications. Let us look at how to export our graphics for use outside of R:

  1. Use one of R's several export functions to convert a graphic into a digital image, it can be done as follows:
    > #use an export function to save a graphic as a digital image
    > #prepare R to export your graphic in one of the following
    formats: pdf, png, jpg, tiff, or bmp
    > #note that your image will be saved into your R working
    directory by default if only a filename is provided
    > #otherwise, your image will be saved to the full provided
    path
    > #optionally, the width and height, in pixels, of the
    resulting image can be specified
    > #export as pdf
    > pdf("myGraphic.pdf", width = 500, height = 500)
    > #OR
    > #export as png
    > png("myGraphic.png", width = 500, height = 500)
    > #OR
    > #export as jpg
    > jpeg("myGraphic.jpg", width = 500, height = 500)
    > #OR
    > #export as tiff
    > tiff("myGraphic.tiff", width = 500, height = 500)
    > #OR
    > #export as bmp
    > bmp("myGraphic.bmp", width = 500, height = 500)
    
  2. Create the graphic, as follows:
    > #create the graphic in R
    > #note that your graphic may NOT be displayed in the graphic
    window during this process
    > #we will use our original fire cost pie chart as an example
    > #use pie(...) to create the pie chart
    > pie(x = pieFireGoldCostSlices,
    labels = pieFireGoldCostLabels, main = pieFireGoldCostMain,
    col = pieFireGoldCostSpecificColors)
    > #use the legend(...) function to add a legend to the chart
    > legend(x = "bottom", legend = pieFireGoldCostLabels,
    fill = pieFireGoldCostSpecificColors)
    
  3. Use dev.off() to close the current device and export your graphic as a digital image:
    > #use dev.off() to close the current device and export the
    graphic as a digital image
    > dev.off()
    
  4. Your graphic will be exported. Verify that your digital image has been created.

What just happened?

We just completed the process of exporting an R graphic as a digital image file. Let us detail the three major steps involved in this procedure.

  1. Prepare the graphic device

    The first step in exporting an R graphic is to prepare the graphic device, which is the entity that handles graphics in R. This step requires that a file type for our exported graphic be defined. Optionally, a width and height for the resulting image can also be specified. These can be accomplished through the use of one of several similar functions. These are:

    • pdf(filename, width, height)
    • png(filename, width, height)
    • jpeg(filename, width, height)
    • tiff(filename, width, height)
    • bmp(filename, width, height)

      Each of these functions prepares the graphic device to export an image associated with its name. For example, the pdf(filename, width, height) function will export an image to PDF format. The filename argument can contain either a complete path specifying where the image is to be saved or just a filename and extension. If only a name and extension are included, the image will be saved to the R working directory. The width and height parameters are measured in pixels and receive a single numeric value. For instance, see the following:

    > pdf("/Users/johnmquick/Desktop/myGraphic.pdf", width = 500,
    height = 500)
    
  2. This would export a 500 by 500 pixel PDF image named myGraphic.pdf to the given user's desktop. Whereas, look at the following:
    > pdf("myGraphic.pdf", width = 300, height = 200)
    
  3. This would export a 300 by 200 pixel PDF image named myGraphic.pdf to the current working directory.
  4. Create the graphic

    The second step is to create the graphic in R. This can be done using any of the techniques that we have explored in this chapter. The only difference between this scenario and our previous activities is that we prepared our graphic device prior to creating our graphic. Note that the graphic must be created after executing one of the functions provided in the previous step in order to be exported. Also, unlike our other experiences with R visuals, your graphic may not be displayed in the graphic window when its function is executed.

  5. Close the graphic device

    The third and final step is to close the graphics device via the dev.off() command. Once dev.off() is executed, the graphic will be exported and saved on your computer as a digital image. Afterwards, be sure to check the location that you specified in the first step to verify that your digital image is present and that it was exported properly.

Remembering these three simple steps will allow you to export your R graphics as digital images, thereby allowing them to be used in other applications.

Pop quiz

  1. In what order must the three steps of the graphic exportation process proceed?

    a. Create the graphic, prepare the graphic device, close the graphic device.

    b. Close the graphic device, prepare the graphic device, create the graphic.

    c. Prepare the graphic device, close the graphic device, create the graphic.

    d. Prepare the graphic device, create the graphic, close the graphic device.

Have a go hero

Create a custom function named exportGraphic that will allow you to save an R graphic as a digital image. Your function should receive five inputs a filename, a filetype, a width, a height, and a graphics function. For instance, exportGraphic should be able to receive the arguments of test.png, png, 500, 500, and barplot(c(1:10)), and yield a PNG image of the specified R graphic. Your function should also be able to export an image of any other valid type. Make sure that your custom function follows the process that we used to export our graphics as digital images. Once created, test your exportGraphic function to ensure that it works as intended.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset