Page Content

Tutorials

Understanding The Base Graphics In R Programming

Introduction to Base Graphics in R

Renowned for its robust and adaptable graphical capabilities, R Programming is a computer language used in statistical computing and data science. R’s “base” or “traditional” graphics system is the cornerstone of its visualization capabilities, despite the existence of multiple sophisticated graphics systems. A whole range of plotting tools are available in this system, ranging from basic scatterplots to intricate, multi-layered visualizations.

The basic graphical concept is straightforward yet effective.This layered technique lets you create a story piece by piece, giving you a lot of versatility. Low-level plotting functions embellish an existing graph with text, lines, or points, while high-level plotting functions generate a new graph on a graphics device like your screen.

Often referred to as the workhorse of R’s base graphics, the plot() function is at the core of this system. It serves as the main instrument for starting a new story. To complement this, you may fine-tune nearly every visual aspect of your visualizations with the par() function, which acts as the main control panel for modifying the default graphical environment. The secret to maximizing the potential of R’s base graphics is to comprehend how these two functions interact.

Creating a Basic Plot with

R has various high-level graphical functions, but plot() is the most basic. Since this function is generic, its plot relies on the object class you supply. Most people use plot() to produce a two-dimensional scatterplot by passing two equal-length numeric vectors as the x and y coordinates of the points. If you provide only one vector x, plot(x) will plot the y-axis values of x against their x-axis indices.

Hist() and boxplot() are R functions for histograms and box-and-whisker displays, respectively. Qplot() from the popular ggplot2 package can be used to create rapid graphs. It must be installed and loaded first. A scatterplot or histogram can be created from two vectors or one vector using qplot().

The plotting function accepts graphical parameters to customize plots. Type specifies points (“p”), lines (“l”), or both (“b”); main, xlab, and ylab establish titles and axis labels; col sets color; pch sets point character style; and xlim and ylim set axis limitations. R lets you build a graph in stages by calling plot() sometimes with type=”n” to create an empty canvas and adding features using low-level charting functions like points(), lines(), abline() (for straight lines), and legend().

Plotting Different Data Types

Most two-dimensional scatterplots are made with plot(). The function plots two equal-length numeric vectors as x and y coordinates on a graph. However, input affects its behavior:

Single Numeric Vector: R’s “workhorse” data structure is a single numeric vector. An organized, one-dimensional collection of numbers. R treats single numbers as atomic vectors of length one, not scalars. If you provide a single number to an object, length() returns 1 and is.vector() returns TRUE.

Data Frame: Plot() creates a scatterplot matrix using a data frame as the primary argument, which is a tabular data format akin to a spreadsheet. This grid of separate scatterplots illustrates the connection between each pair of data frame columns.

Factor Variable: Plotting a numeric vector against a factor (a variable that represents categories) automatically creates side-by-side boxplots. These are great for analyzing how a numeric variable is distributed among various groups.

Customizing the Plot’s Appearance

R’s base graphics system and added packages like ggplot2 let you alter charts considerably. plot(), a generic function that generates plots based on input class, is base R’s main charting function. Adding graphical parameters as input changes the appearance. Type controls plot type (“p” for points, “l” for lines), main, xlab, and ylab for titles and axis labels, col for color, pch for point character, and lty and lwd for line type and width.

Axis limitations can be established with xlim and ylim. After making an initial plot, use low-level plotting commands like points(), lines(), abline() (for straight lines), text(), and legend() to add features. Use the par() function to set parameters like mfrow for multiple plots or bg for the background color to update all visuals on a device.

The “grammar of graphics”-based ggplot2 package is popular and strong. Creating plots is easy using its “quick plot” method, qplot(). In ggplot2, layers are created by adding geoms (geometric objects like geom_point() or geom_line()) to a base ggplot() or qplot() object using the + operator. These geoms allow appearance constants (e.g., color=”red”). can map data variables to visual attributes like color, shape, size, and fill with the aes() method. This automatically makes legends for data groups. Using facets, ggplot2 may create numerous subplots for a categorical variable. Use programs like rgl and its plot3d() function to rotate and zoom interactive 3D charts using a mouse.

Building a Graph in Layers

Base graphics’ real strength is in its capacity to create plots gradually. You can use a set of low-level charting functions to add more information after generating an initial plot with a high-level function like plot(). Instead of constructing a new graph, these functions overlay the one that is already active. A few crucial low-level operations are as follows:

points(): R’s low-level points() function adds points or connected lines to a visualization. It builds on the current graph, letting you develop a plot gradually. Add points using points(x, y), where x and y are vectors with point coordinates. A high-level function like plot(), sometimes with type=”n” to draw only the axes and create an empty canvas, creates an initial charting area, and then points() adds the data points.

lines(): R’s low-level lines() function adds connected lines to a plot. It builds on a plot started with plot() rather than creating a new graph. Creating an empty plot with plot(…, type=”n”) to set the axes and coordinate system and adding graphical elements with lines() is a typical method.

abline(): Abline() is a low-level R plotting command that adds straight lines to a graphic. It enhances an existing graph. A versatile function can draw lines in multiple ways. The simplest technique is to define an intercept and slope with abline(a, b), which draws y = a + bx. Draw horizontal and vertical lines by giving their positions with h and v. The abline(h=y) and abline(v=x) functions add horizontal and vertical lines to the given y- and x-coordinates, respectively. This helps mark plot values like mean and median.

text(): Text annotations are added to plots using R’s low-level text() function. It builds on the current graph to add text at coordinate points. Text(x, y, labels,…) is the fundamental syntax, where x and y are the text placement coordinates and labels is a character vector with the plotted text. R element-wise plots labels[i] at (x[i], y[i]) if x, y, and labels are vectors. The function uses point indices (1:length(x)) as labels if labels is omitted.

legend(): The low-level plotting tool legend() adds a legend to a graph in R. Labeling plotting characters, line styles, colors, and other visual aspects helps interpret multicomponent plots. You can place the legend by specifying x and y coordinates for its top-left corner, using a keyword string like “bottomleft”, “topright”, or “topleft”, or interactively by clicking on the plot with the locator(1) function.

With this multi-layered method, you may begin with a simple scatterplot and gradually add trend lines, reference points, textual annotations, and a descriptive legend to give you total control over the finished product.

Understanding Graphical Parameters

Understand R graphics parameters to customize graphs beyond basic defaults. These settings regulate colors, line styles, text justification, and figure organization with many alternatives. There are two main settings. First, send them as named arguments to high-level plotting procedures like plot(), hist(), or boxplot() or low-level functions like points(), lines(), abline(), text(), and legend() that add elements to an existing plot to temporarily configure them. Second, you can set them permanently for the current graphics device using the par() function. These parameters will influence all subsequent graphics functions until adjusted or the device is closed.

A variety of parameters can be customized. You can use type to set the plot type (“p” for points, “l” for lines, or “n” for an empty plot). Set titles and axis labels with main, sub, xlab, ylab. Point character style (pch), symbol size (cex), line type (solid, dashed, etc.), and line width (lwd) control point and line appearance. Point and line colors are col and background colors are bg.

Setting limitations with xlim and ylim, label orientation with las, and suppressing axis drawing with xaxt or yaxt are also possible. Set in par(), parameters like mfrow and mfcol allow many graphs on one device. R’s fundamental graphics system relies on these parameters, although the lattice and ggplot2 packages use other parameter systems. A complete list of fundamental graphics parameters is in par() documentation.

Key Graphical Parameters

You can use par() or plot() to change several typical graphical parameters:

Colors (col, bg): The most basic graphical parameters in R are col and bg, which regulate graphic colors. Plotted elements including points, lines, text, axes, and titles are often colored using the col option. You can specify col colors by integer, where R uses a default palette of 8 colors (e.g., 1 for black, 2 for red) that recycles for numbers greater than 8; by name as a character string, with over 650 built-in names like “blue” or “seagreen4” available via the colors() function; or by using color palettes from functions like rainbow() or packages like RColorBrewer that generate a vector of colors.

Point and Line Styles (pch, lty, lwd, cex): Point and Line Styles These choices adjust plotted items’ appearance.

  • Point Character (pch) displays circles, squares, and triangles.
  • Lty (Line Type) specifies solid, dashed, dotted, or other line patterns.
  • Line thickness is controlled by lwd (Line Width).
  • Point and text sizes are changed in relation to the default size using cex (Character Expansion).

Axes and Boxes (bty, las, xaxt, yaxt): Bty (Box Type) allows you to customize an L-shaped or U-shaped box around the plot. Las controls the orientation of the axis labels (horizontal, vertical, etc.). To build new axes, set xaxt or yaxt to “n” to suppress the default ones, and then use the low-level axis() method to add your own.

Layout and Margins (mfrow, mar): The par() function is particularly useful for organizing several plots on a single graphic. The plotting device can be divided into a grid of rows and columns using the mfrow option, for instance. Subsequent plots will fill this grid in a row-wise manner. Additionally, the mar parameter, which determines the size of the margins, allows you to regulate the space surrounding your plot.

When using par() to perform global modifications, it is customary and strongly advised to save the initial configurations first. Assigning an object to the par() function’s output allows you to accomplish this. By using that stored object to use par() again once you’re done with your custom plots, you can make sure your modifications don’t inadvertently impact any other graphs you might make later in the session.

Conclusion

For data visualization, R Programming is fundamental graphics system provides a stable and incredibly adaptable environment. Plot() is a flexible starting point that may be used to create a variety of informative graphs by cleverly adjusting to various data types. Combining this with low-level plotting algorithms creates elaborate layer-by-layer images. The par() function also lets you customize layout, margins, colors, and fonts. Mastering these key techniques will allow you to use R’s expressive power to create publications-quality, lucid, and purpose-driven graphics.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index