ggplot2 in R Programming
To add components to a graph, R’s low-level plotting functions are essential. High-level functions like plot() are needed to edit or improve graphs when their default output doesn’t meet user visualization needs. Complex plots can be gradually constructed on an active graphics device by sequentially applying these commands.
Using a high-level command to first generate an empty plotting region is a standard procedure for building complex graphs. Plot() is usually called with the argument type=”n” (which stands for “No plotting at all”) to accomplish this. In order to create the perfect canvas for next low-level operations, this command makes sure that the graphics device is opened (or refreshed) and that the coordinate system and axes are configured in accordance with the data without drawing any points or lines. A base R Programming graph can be enhanced using the main low-level plotting methods points(), lines(), abline(), text(), and legend().
Adding Points ()
The purpose of the points(x, y) function is to supplement the graph that is currently visible with a set of (x, y) coordinates. The coordinates of the points to be plotted are specified by two vectors, x and y, of equal length.
With the use of graphical factors that regulate color, size, and character type, the points can be greatly customized.
Point Character (pch): In R, the Point Character ($\text{pch}$) argument defines the shape of data points in plotting functions like plot() and low-level commands like points(). Set the value for the named input $\text{pch}$ to personalize the appearance of points on a graph.
This parameter can be an integer value (1-25 inclusive) or a single character surrounded in quotation quotes (e.g., $\text{pch}=”*”$, $\text{pch}=”?”$, $\text{pch}=”.”$, or $\text{pch}=”X”$). To indicate observations in a scatter plot using plus signs ($\text{+}$), use $\text{pch}=”+”$ in the points() function.
Point Size (cex): Character expansion, or cex, can be used to change the plotted points’ size. Half-size points are produced when a number such as cex=0.5 is specified, but cex=2.3 asks points that are noticeably more than the default.
Color (col): You can use col to set the color of the points by passing in an integer selector or a string name consisting of characters (such as “seagreen4”).
To plot particular groups of points differently depending on circumstances, such as plotting points where $y \ge 5$ using a distinct color and character, the points() function is commonly used after logically subsetting data vectors.
Adding Lines ()
The lines(x, y) function connects the points defined by the input vectors x and y by adding connected lines to the existing graphic. When displaying time series data or trends where it makes sense to connect observations sequentially, this function is quite helpful.
Line look is the main customizing focus:
Line Type (lty): Dashed, dotted, or solid lines are determined by this parameter. The integers 1–6 are accepted. For instance, lty=4 can be used to make a dash-dot-dash line.
Line Width (lwd): The Line Width ($\text{lwd}$) option controls line thickness in R graphics. This argument applies to plot(), lines(), abline(), and segments(), as well as other line-drawing functions. Specifying a number for $\text{lwd}$ multiplies the default line thickness of $1$. The $\text{lwd}=2$ parameter specifies double the default thickness of lines.
Color (col): In R plotting, and more especially in the low-level plotting function $\text{lines}()$, the Color ($\text{col}$) parameter is an essential graphical setting that determines the color of the connected segments added to an existing graph.
The segments(x0, y0, x1, y1) function replaces lines() to draw lines linking coordinates without transcending the plotting zone that do not follow a straight-line formula. Segments() draws lines from $(x_0, y_0)$ to $(x_1, y_1)$.
Adding Straight Lines ()
Straight lines can be added to an existing plot using the abline() method. It supports a number of typical use scenarios and is incredibly flexible:
Intercept and Slope: A simple linear regression model has two parameters: the intercept ($\beta_0$) and the slope ($\beta_1$). The regression coefficients are $\beta_0$ and $\beta_1$ taken together. Linear regression models define a function that estimates the mean of a response variable ($Y$) given an explanatory variable ($X$). In the fitted model, $\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 x$, where $\hat{\beta}_0$ and $\hat{\beta}_1$ represent the estimated population parameters.
Horizontal Lines: To add horizontal lines to a R plot, use the low-level plotting command $\text{abline}$. This function is useful for adding straight lines and creating horizontal lines throughout the plotting region using $\text{h}=\text{y}$. Here, $\text{y}$ indicates the y-coordinate for drawing the horizontal line.
Vertical Lines: Use the low-level plotting command $\text{abline}()$ to add straight lines to a R plot. To draw vertical lines on the graph, use $\text{v}=\text{x}$ in the $\text{abline}$ function. In this example, $\text{x}$ indicates the x-coordinate (abscissa) for the vertical line.
Regression Lines: abline() automatically extracts the coefficients (slope and intercept) from the model object and superimposes the fitted regression line onto the plot if it is supplied with an object created by the linear regression function lm() (for example, abline(lm.obj)).
Straight lines are styled using graphical factors like lty, lwd, and col.
Adding Text ()
By giving the coordinates $(x, y)$, the text(x, y, labels,…) function enables the user to insert any text wherever on the current graph. The labels argument is used to supply the text content as a character string or vector.
The text string is centered over the designated $(x, y)$ coordinate by default. To add a line break, character strings can use escape sequences like \n. Cex is used to regulate text size.
The text() function complements expression() for specialized annotations, particularly in scientific or statistical contexts:
Greek Symbols: Greek symbols are used in scientific and mathematical R Programming plotting to annotate graphs. Use the $\text{expression}$ function to add these symbols to titles, labels, or other plot text. R’s $\text{expression}()$ function can invoke the $\text{plotmath}$ mode, which understands Greek letters and mathematical markup syntax.
Mathematical Expressions: The Expression() function can typeset complex formulas like fractions, superscripts (^), and subscripts ([ ]) using a LATEX-like markup language. The paste() function embedded within expression() allows you to blend regular text with symbols.
The interactive function locator(n) can be used to help with accurate placement, particularly when figuring out coordinates for intricate textual annotations. location() provides the $(x, y)$ coordinates of the click(s) after waiting for the user to click on the plot window up to $n$ times. To guarantee precise positioning, this returned set of coordinates can be supplied straight to text(). Text(locator(1),”label”), for instance, enables text placement on a single mouse click.
When placing text close to the margins, it’s critical to control clipping. Drawing is clipped to the plot region by default; however, text can be positioned outside of the main plotting area by setting the graphics option xpd to TRUE (allowing drawing into the figure margins) or NA (allowing drawing into all margins, including outside margins).
Including a Legend ()
In multi-layered or multi-group plots, the legend(x, y, legend,…) function is essential for providing a key to comprehend items.
Positioning: The user coordinates $(x, y)$ can be used to specifically define the legend box’s placement. Or, to rapidly tie the legend to a corner, R offers handy placement strings like “topleft”, “bottomright”, etc.
Content and Mapping: The descriptive labels must be provided by the user as a character vector in the legend argument. Importantly, other graphical parameters must be supplied as vectors of the same length as the legend vector in order to visually connect these labels to the lines or symbols on the graph. To maintain the required equal length and guarantee effective element-wise matching, the value NA must be used in the parameter vector’s place if a visual property is irrelevant to a particular legend entry.
The character expansion factor for the points that are shown inside the legend box is explicitly controlled by additional parameters like pt.cex, which enables their size to be independent of the text size that cex controls.
For instance, the lty vector might be $c(2, NA)$ and the pch vector might be $c(NA, 15)$ to describe a plot that shows both a dashed line (entry 1) and a square point (entry 2). In this case, 15 is a square point character. Specialized functions like colorlegend() (available in the shape package) may be chosen for legends referencing continuous data indexed by color because they draw a seamless color strip rather than discrete symbols.
When combined, these low-level commands offer users fine-grained control over a plot’s graphical components, enabling them to go beyond the defaults and produce unique, educational visualizations.