Session Ready
Exercise

Saving plot results as files

In an interactive R session, we typically generate a collection of different plots, often using the results to help us decide how to proceed with our analysis. This is particularly true in the early phases of an exploratory data analysis, but once we have generated a plot we want to share with others, it is important to save it in an external file.

R provides support for saving graphical results in several different external file formats, including jpeg, png, tiff, or pdf files. In addition, we can incorporate graphical results into external documents, using tools like the Sweave() function or the knitr package. One particularly convenient way of doing this is to create an R Markdown document, an approach that forms the basis for another DataCamp course.

Because png files can be easily shared and viewed as e-mail attachments and incorporated into many slide preparation packages (e.g. Microsoft Powerpoint), this exercise asks you to create a plot and save it as a png file. The basis for this process is the png() function, which specifies the name of the png file to be generated and sets up a special environment that captures all graphical output until we exit this environment with the dev.off() command.

Instructions
100 XP
  • Use the png() function to direct all subsequent plot results to the external file bubbleplot.png.
  • Run the code to re-create the second bubble plot from the previous exercise.
  • Exit the png() environment to return graphics control to your session by calling dev.off().
  • Use the list.files() code provided to verify that bubbleplot.png was created. To do this, you need to specify "png" as the value to the pattern argument to list.files().