Graphics Devices with R

R Graphics Devices

R graphics devices are essential for creating and managing graphical output in R. They allow you to direct your plots to various output formats, such as screens, files, and more. Here’s a detailed guide on R graphics devices.

Types of Graphics Devices

  • Screen Devices

Screen devices display graphics directly on your monitor. They are interactive and used for on-the-fly visualization.

  • windows() (Windows only): Opens a new graphics window.
  • x11() (Unix-like systems): Opens a new X11 window.
  • quartz() (macOS only): Opens a new graphics window in Quartz.

Example: Using windows()

windows()  # Opens a new graphics window on Windows
plot(1:10, 1:10)

File Devices

File devices save your plots to files in various formats. You can specify the format using the device function.

  • pdf(): Creates a PDF file.
  • png(): Creates a PNG file.
  • jpeg(): Creates a JPEG file.
  • tiff(): Creates a TIFF file.
  • svg(): Creates an SVG file.

Example: Saving a Plot to a PNG File 

png("plot.png")  # Opens a PNG file device
plot(1:10, 1:10)  # Create a plot
dev.off()  # Closes the PNG device and saves the file

 Common Graphics Device Functions

  • dev.new(): Opens a new graphics window (similar to windows() or x11() but more general).
  • dev.off(): Closes the current graphics device. Always call this after plotting to a file to save and close the file.
  • dev.list(): Lists all open graphics devices.
  • dev.cur(): Returns the number of the current graphics device.

Example: Listing and Closing Devices 

# List all open devices
dev.list()
# Close the current device
dev.off()

Device-Specific Parameters

Each graphics device function has parameters that control the output:

  • width and height: Dimensions of the output (in inches for most devices).
  • res: Resolution (for raster devices like PNG and JPEG, in dots per inch).
  • bg: Background color (for raster devices).

Example: Creating a High-Resolution PNG 

png("high_res_plot.png", width = 800, height = 600, res = 150, bg = "white")
plot(1:10, 1:10)
dev.off()

 Customizing Device Output

You can control how plots are rendered on each device:

  • par(): Adjust graphical parameters such as margins, layout, and text size.
  • layout(): Define multiple plots on a single page.
  • mfrow and mfcol: Set up multiple plots in a matrix layout.

Example: Multiple Plots in One File 

# Save multiple plots to a PDF
pdf("multiple_plots.pdf")
par(mfrow = c(2, 2))  # 2x2 matrix of plots
plot(1:10, 1:10)
plot(10:1, 1:10)
plot(1:10, (1:10)^2)
plot((1:10)^2, (1:10)^3)
dev.off()

Interactive Devices

For interactive or real-time graphics, consider using packages like shiny for web applications or rgl for 3D plotting.

Example: Using rgl for 3D Plotting 

library(rgl)
open3d()  # Opens a new 3D plotting window
plot3d(rnorm(100), rnorm(100), rnorm(100))

Advanced Use Cases

You can combine different devices and save plots in various formats. For instance, generating plots in PNG and then converting them to PDF.

Example: Converting PNG to PDF 

# Save plot as PNG
png("plot.png")
plot(1:10, 1:10)
dev.off()
# Convert PNG to PDF using a tool like ImageMagick (from the command line)
# convert plot.png plot.pdf

 Summary

  • Screen Devices: Use windows(), x11(), or quartz() for on-screen graphics.
  • File Devices: Use pdf(), png(), jpeg(), tiff(), svg() to save plots to files.
  • Device Functions: dev.new(), dev.off(), dev.list(), dev.cur() manage graphics devices.
  • Device Parameters: Control output with parameters like width, height, res, bg.
  • Customizing Output: Use par(), layout(), mfrow, and mfcol for detailed customization.
  • Interactive Devices: Use packages like shiny and rgl for interactive and 3D plots.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print