R Plot

plot is a built-in R function for plotting.


Example

x <- seq(0, 2*pi, length.out=100)
y <- sin(x)
plot(x,y)


Plots

The plot function takes some object and creates a plot.

Most generically, this function takes two numeric vectors and creates a scatterplot.

Configuration

There are many optional arguments to the plot function. These include:

density

Kernel density estimates can be plotted using the plot function. Try:

plot(density(x, na.rm=TRUE), main="Distribution of x")

lm

Linear model estimates have several plots associated.

  1. a Tukey-Anscombe (a.k.a. residuals vs. fitted) plot

  2. a residuals Q-Q plot

  3. a scale-location plot
  4. a Cook's distance plot
  5. a residuals vs. leverage plot
  6. a Cook's distance vs leverage plot

These plots are selected using the which option. For example:

plot(lm(y ~ x), which=1)

emmeans

The pairs function calculates pairwise comparisons. The confidence intervals can be plotted like:

plot(pairs(emmeans(mod, spec = ~ treatment)))


Lines

The abline function bundles a regression line into a plot. Try:

plot(x, y)
abline(lm(y ~ x))

This function takes some of the same options as above; in particular col and lty.


Legends

The legend function bundles a legend into a plot. The first arguments (one or two) set the position. These can be coordinates (in the same units as the plot itself), or it can be a single descriptive word ("topleft", "topright", "bottomleft", or "bottomright").

For example, with two overlaid plots, try:

plot(x, y1, pch=1, col="red")
plot(x, y2, pch=0, col="blue")
legend("topright", pch=c(1,0), col=c("red", "blue"), legend=c("y1", "y2"), text.col=c("red", "blue"))

Beyond that, the legend function takes these options:


CategoryRicottone

R/BuiltinFunctions/Plot (last edited 2026-09-03 15:02:15 by DominicRicottone)