In R, graphics devices are the interfaces through which R plots are rendered. If you understand graphics devices it’s crucial for creating and customizing plots effectively.
There are primarily two types of graphics devices in R: screen devices and file devices.
1. Screen Devices
Screen devices, as the name suggests, render plots directly onto the screen. The primary screen device in R is the default graphics device, usually a window displaying the plot.
Example
# Open a new graphics device (screen device) x <- c(1, 2, 3, 4) y <- c(1, 4, 9, 16) plot(x, y) # Plot displayed on the screen
2. File Devices
File devices render plots to external files in various formats such as PNG, PDF, JPEG, etc. This is useful for saving plots for later use or embedding them in documents.
Example
# Open a new graphics device (file device) png("plot.png") x <- c(1, 2, 3, 4) y <- c(1, 4, 9, 16) plot(x, y) # Plot rendered to the file "plot.png" dev.off() # Close the file device
Graphics parameters in R control various aspects of plot appearance such as colors, line types, point types, etc. They can be set globally for all subsequent plots or locally for individual plots.
Let me explain the commonly used graphics parameters in R.
- pch – Sets the plotting symbol (point type).
- col – Sets the color of plotting symbols, lines, and text.
- lty – Sets the line type (solid, dashed, etc.).
- lwd – Sets the line width.
- main – Sets the main title of the plot.
- xlab, ylab – Set the labels for the x and y-axis respectively.
Now I’ll give you an example demonstrating the use of some of these graphics parameters.
# Set up some data x <- 1:10 y <- x^2 # Customize plot appearance using graphics parameters plot(x, y, type = "b", # Plot both points and lines pch = 16, # Point type (filled circle) col = "blue", # Color of points and lines lty = 2, # Line type (dashed) lwd = 2, # Line width main = "Squared Values", # Main title xlab = "X", # X-axis label ylab = "Y") # Y-axis label
In this example, I’m plotting the squared values of `x`, customizing the appearance of the plot with various graphics parameters.
Example – Customizing a Scatter Plot
# Generate random data set.seed(123) x <- rnorm(100) y <- rnorm(100) # Create a scatter plot with customized appearance plot(x, y, main = "Customized Scatter Plot", # Main title xlab = "X-axis", ylab = "Y-axis", # Axis labels col = "darkgreen", # Point color pch = 20, # Point type (solid circle) cex = 1.5, # Point size xlim = c(-3, 3), ylim = c(-3, 3), # Set limits for x and y axes bg = "lightblue", # Background color for points las = 1) # Orientation of axis labels (horizontal)
Let me explain the above code.
- `set.seed(123)`: Sets a seed for reproducibility.
- `rnorm(100)`: Generates 100 random numbers from a normal distribution.
- `plot(x, y, …)`: Creates a scatter plot with `x` and `y` data.
- `main`, `xlab`, `ylab`: Set the main title and axis labels.
- `col`: Sets the color of points to dark green.
- `pch`: Sets the point type to a solid circle.
- `cex`: Adjusts the size of points to 1.5 times their default size.
- `xlim`, `ylim`: Sets the limits for the x and y axes.
- `bg`: Sets the background color for points to light blue.
- `las`: Sets the orientation of axis labels to horizontal.
With these examples, as a beginner you understood that graphics devices and parameters in R is crucial for effective data visualization.
You can easily customize where plots are rendered and customizing their appearance, you can create informative and visually appealing visualizations using R.