Base R graphics offer powerful tools for data visualization. They provide a foundation for creating essential plot types like scatterplots, line plots, and histograms. These basic plots are crucial for initial data exploration and understanding relationships between variables.
Customization options in base R graphics allow you to fine-tune plot aesthetics. You can adjust colors, symbols, labels, and axis limits to create clear, informative visualizations. These skills are fundamental for effective data presentation and analysis in R.
Basic Plots in R
Creating Scatterplots, Line Plots, and Bar Plots
- Create scatterplots using the
plot()
function to display the relationship between two continuous variables- Specify the x-axis and y-axis variables using the
x
andy
arguments respectively (e.g.,plot(x = height, y = weight)
) - Scatterplots are useful for identifying patterns, trends, or correlations between variables
- Example:
plot(x = iris$Sepal.Length, y = iris$Sepal.Width)
creates a scatterplot of sepal length vs. sepal width for the iris dataset
- Specify the x-axis and y-axis variables using the
- Produce line plots using the
plot()
function with thetype = "l"
argument to connect data points with lines- Line plots are effective for visualizing trends or changes over time (e.g., stock prices, temperature readings)
- Example:
plot(x = time, y = temperature, type = "l")
creates a line plot of temperature readings over time
- Generate bar plots using the
barplot()
function to display the heights of bars corresponding to values in a vector or matrix- The height of each bar represents the value of a variable for a specific category or group (e.g., sales by product category)
- Example:
barplot(height = sales, names.arg = products)
creates a bar plot of sales for different product categories
Histograms and Box Plots
- Create histograms using the
hist()
function to visualize the distribution of a single continuous variable- Histograms divide the data into bins and show the frequency or density of observations in each bin
- Useful for understanding the shape, center, and spread of a distribution (e.g., normal distribution, skewed distribution)
- Example:
hist(x = heights)
creates a histogram of height measurements
- Produce box plots using the
boxplot()
function to summarize the distribution of a continuous variable across different categories or groups- Box plots display the median, quartiles, and potential outliers of a variable
- Useful for comparing the distribution of a variable across different levels of a categorical variable (e.g., test scores by gender)
- Example:
boxplot(score ~ gender, data = test_results)
creates box plots of test scores for each gender category
Plot Customization
Modifying Colors, Symbols, and Labels
- Customize the color of plot elements using the
col
argument- Specify colors using color names (e.g.,
col = "red"
), hexadecimal codes (e.g.,col = "#FF0000"
), or a color palette (e.g.,col = rainbow(n)
) - Example:
plot(x, y, col = "blue")
creates a scatterplot with blue points
- Specify colors using color names (e.g.,
- Change the plotting character or symbol for points in a scatterplot using the
pch
argument- Different values of
pch
correspond to different symbols (e.g.,pch = 16
for filled circles,pch = 17
for filled triangles) - Example:
plot(x, y, pch = 17)
creates a scatterplot with filled triangle points
- Different values of
- Add a title to the plot using the
main
argument and labels for the x-axis and y-axis usingxlab
andylab
arguments respectively- Example:
plot(x, y, main = "Scatterplot", xlab = "X-axis", ylab = "Y-axis")
adds a title and axis labels to the plot
- Example:
Adjusting Axis Limits, Element Sizes, and Line Widths
- Control the limits of the x-axis and y-axis using the
xlim
andylim
arguments respectively- Specify the minimum and maximum values for each axis as a vector (e.g.,
xlim = c(0, 100)
) - Useful for zooming in on a specific region of the plot or ensuring consistent axis ranges across multiple plots
- Example:
plot(x, y, xlim = c(0, 50), ylim = c(0, 100))
sets the x-axis limits to 0-50 and y-axis limits to 0-100
- Specify the minimum and maximum values for each axis as a vector (e.g.,
- Adjust the size of plot elements (e.g., points, text) using the
cex
argument- Values greater than 1 increase the size, while values less than 1 decrease the size relative to the default
- Example:
plot(x, y, cex = 1.5)
creates a scatterplot with points 1.5 times larger than the default size
- Modify the line width for lines or the border width for other plot elements using the
lwd
argument- Larger values result in thicker lines or borders
- Example:
plot(x, y, type = "l", lwd = 2)
creates a line plot with a line width of 2
Multiple Plots in R
Creating Plot Grids with par() and layout()
- Use the
par()
function to set graphical parameters that control the layout and appearance of plots- Modify parameters to create a grid of plots or customize the arrangement of multiple plots within a single figure
- Example:
par(mfrow = c(2, 2))
creates a 2x2 grid of plots, filling the grid row-wise - Example:
par(mfcol = c(2, 2))
creates a 2x2 grid of plots, filling the grid column-wise
- Utilize the
layout()
function for more flexible arrangement of multiple plots within a single figure- Takes a matrix as input, where each element represents a cell in the grid of plots
- The value of each matrix element specifies the order in which the plots should be drawn
- Example:
layout(matrix(c(1, 1, 2, 3), 2, 2))
creates a layout with two plots in the first row and two plots in the second row
- Include zeros in the layout matrix to represent empty cells in the grid, allowing for complex layouts with plots of different sizes and shapes
- Example:
layout(matrix(c(1, 1, 0, 2), 2, 2))
creates a layout with one large plot on the left and one smaller plot on the bottom right
- Example:
Saving Plots in R
Saving Plots as Image Files
- Save plots as PNG, JPEG, or PDF files using the
png()
,jpeg()
, orpdf()
functions respectively- These functions create a new graphics device that redirects the plot output to a file instead of the default plot window
- Specify the file name and path as the first argument (e.g.,
png("plot.png")
) - Example:
png("scatterplot.png"); plot(x, y); dev.off()
saves the scatterplot as a PNG file named "scatterplot.png"
- Control the dimensions of the output image using the
width
andheight
arguments- For
png()
andjpeg()
, the dimensions are specified in pixels - For
pdf()
, the dimensions are specified in inches - Example:
jpeg("plot.jpg", width = 800, height = 600)
saves the plot as a JPEG file with dimensions 800x600 pixels
- For
- Adjust the resolution of the output image using the
resolution
argument inpng()
andjpeg()
- Specified in dots per inch (DPI)
- Higher values result in better quality images but larger file sizes
- Example:
png("plot.png", width = 800, height = 600, resolution = 300)
saves the plot as a high-resolution PNG file
Closing Graphics Devices and Saving ggplot2 Plots
- Close the graphics device using the
dev.off()
function after saving a plot- Ensures that the file is properly closed and saved
- Example:
png("plot.png"); plot(x, y); dev.off()
- Save plots created with ggplot2 using the
ggsave()
function from the ggplot2 package- Automatically determines the appropriate file format based on the file extension
- Allows control over plot size, resolution, and other output settings
- Example:
ggsave("plot.pdf", width = 8, height = 6, dpi = 300)
saves the current ggplot2 plot as a PDF file with specified dimensions and resolution