0% found this document useful (0 votes)
20 views10 pages

Graphics Devices in R Graphics Devices in R

Graphics devices determine where plots appear - common devices include the screen, PDF files, and image files. The screen device shows plots on the computer monitor and is best for exploratory analysis, while file devices allow incorporating plots into documents. Vector formats like PDF are suitable for line plots and resize well, while bitmap formats like PNG work for plots with many points but do not resize gracefully. R users must explicitly direct plots to a particular graphics device.

Uploaded by

niti860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Graphics Devices in R Graphics Devices in R

Graphics devices determine where plots appear - common devices include the screen, PDF files, and image files. The screen device shows plots on the computer monitor and is best for exploratory analysis, while file devices allow incorporating plots into documents. Vector formats like PDF are suitable for line plots and resize well, while bitmap formats like PNG work for plots with many points but do not resize gracefully. R users must explicitly direct plots to a particular graphics device.

Uploaded by

niti860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Graphics Devices in R Graphics Devices in R

Roger D. Peng, Associate Professor of Biostatistics


Johns Hopkins Bloomberg School of Public Health
What is a Graphics Device? What is a Graphics Device?
A graphics device is something where you can make a plot appear
When you make a plot in R, it has to be "sent" to a specic graphics device
The most common place for a plot to be "sent" is the screen device

A window on your computer (screen device)


A PDF le (le device)
A PNG or JPEG le (le device)
A scalable vector graphics (SVG) le (le device)
-
-
-
-

On a Mac the screen device is launched with the quartz()


On Windows the screen device is launched with windows()
On Unix/Linux the screen device is launched with x11()
-
-
-
2/10
What is a Graphic Device? What is a Graphic Device?
When making a plot, you need to consider how the plot will be used to determine what device the
plot should be sent to.
For quick visualizations and exploratory analysis, usually you want to use the screen device
For plots that may be printed out or be incorporated into a document (e.g. papers/reports, slide
presentations), usually a le device is more appropriate
NOTE: Not all graphics devices are available on all platforms (i.e. you cannot launch the
windows() on a Mac)

The list of devices is found in ?Devices; there are also devices created by users on CRAN -

Functions like plot in base, xyplot in lattice, or qplot in ggplot2 will default to sending a
plot to the screen device
On a given platform (Mac, Windows, Unix/Linux) there is only one screen device
-
-

There are many different le devices to choose from -

3/10
How Does a Plot Get Created? How Does a Plot Get Created?
There are two basic approaches to plotting. The rst is most common:
1. Call a plotting function like plot, xyplot, or qplot
2. The plot appears on the screen device
3. Annotate plot if necessary
4. Enjoy
library(datasets)
with(faithful, plot(eruptions, waiting)) ## Make plot appear on screen device
title(main = "Old Faithful Geyser data") ## Annotate with a title
4/10
How Does a Plot Get Created? How Does a Plot Get Created?
The second approach to plotting is most commonly used for le devices:
1. Explicitly launch a graphics device
2. Call a plotting function to make a plot (Note: if you are using a le device, no plot will appear on
the screen)
3. Annotate plot if necessary
4. Explicitly close graphics device with dev.off() (this is very important!)
pdf(file = "myplot.pdf") ## Open PDF device; create 'myplot.pdf' in my working directory
## Create plot and send to a file (no plot appears on screen)
with(faithful, plot(eruptions, waiting))
title(main = "Old Faithful Geyser data") ## Annotate plot; still nothing on screen
dev.off() ## Close the PDF file device
## Now you can view the file 'myplot.pdf' on your computer
5/10
Graphics File Devices Graphics File Devices
There are two basic types of le devices: vector and bitmap devices
Vector formats:
pdf: useful for line-type graphics, resizes well, usually portable, not efcient if a plot has many
objects/points
svg: XML-based scalable vector graphics; supports animation and interactivity, potentially useful
for web-based plots
win.metafile: Windows metale format (only on Windows)
postscript: older format, also resizes well, usually portable, can be used to create
encapsulated postscript les; Windows systems often dont have a postscript viewer

6/10
Graphics File Devices Graphics File Devices
Bitmap formats
png: bitmapped format, good for line drawings or images with solid colors, uses lossless
compression (like the old GIF format), most web browsers can read this format natively, good for
plotting many many many points, does not resize well
jpeg: good for photographs or natural scenes, uses lossy compression, good for plotting many
many many points, does not resize well, can be read by almost any computer and any web
browser, not great for line drawings
tiff: Creates bitmap les in the TIFF format; supports lossless compression
bmp: a native Windows bitmapped format

7/10
Multiple Open Graphics Devices Multiple Open Graphics Devices
It is possible to open multiple graphics devices (screen, le, or both), for example when viewing
multiple plots at once
Plotting can only occur on one graphics device at a time
The currently active graphics device can be found by calling dev.cur()
Every open graphics device is assigned an integer .
You can change the active graphics device with dev.set(<integer>) where <integer> is the
number associated with the graphics device you want to switch to

8/10
Copying Plots Copying Plots
Copying a plot to another device can be useful because some plots require a lot of code and it can
be a pain to type all that in again for a different device.
NOTE: Copying a plot is not an exact operation, so the result may not be identical to the original.
dev.copy: copy a plot from one device to another
dev.copy2pdf: specically copy a plot to a PDF le

library(datasets)
with(faithful, plot(eruptions, waiting)) ## Create plot on screen device
title(main = "Old Faithful Geyser data") ## Add a main title
dev.copy(png, file = "geyserplot.png") ## Copy my plot to a PNG file
dev.off() ## Don't forget to close the PNG device!
9/10
Summary Summary
Plots must be created on a graphics device
The default graphics device is almost always the screen device, which is most useful for
exploratory analysis
File devices are useful for creating plots that can be included in other documents or sent to other
people
For le devices, there are vector and bitmap formats

Vector formats are good for line drawings and plots with solid colors using a modest number
of points
Bitmap formats are good for plots with a large number of points, natural scenes or web-
based plots
-
-
10/10

You might also like