Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

If you've spent any time in the R community, you've likely heard that ggplot2 is the only way to make "real" graphs. Because of this, many learners approach Base R plotting with a specific misconception: they treat the plot() function as if it's an additive tool, similar to how you add layers in ggplot2 using the + operator.

The Myth that plot() is an Additive Function

I see this all the time. A student will try to plot a set of data, and then, wanting to add a second line or a set of points on top of it, they call plot() a second time. They expect the two plots to merge. Instead, the second call completely wipes the first one off the screen.

# The "Wrong" way: thinking plot() adds layers
x <- 1:10
y1 <- x^2
y2 <- x^2 + 10

plot(x, y1, type = "l", col = "blue")
plot(x, y2, type = "l", col = "red") 
# Result: The blue line is gone. You only see the red line.

In Base R, plot() is a "high-level" plotting function. Its first job is to initialize a new graphics device—essentially, it throws away the old canvas and puts down a fresh, blank sheet of paper with axes and labels already drawn. If you want to add more data to an existing plot, you can't use plot() again.

The Canvas Approach: High-Level vs. Low-Level Functions

To actually build a complex visualization in Base R, you have to think like a painter. You use one high-level function to set up the canvas, and then a series of "low-level" functions to paint details onto that canvas. These low-level functions don't create new plots; they only know how to add elements to the current active plot.

The most common low-level functions are lines(), points(), and abline(). Here is how you actually handle the example from above:

# The "Right" way: Setting the canvas, then painting
x <- 1:10
y1 <- x^2
y2 <- x^2 + 10

# 1. Setup the canvas (High-level)
plot(x, y1, type = "l", col = "blue", lwd = 2, 
     main = "Growth Comparison", xlab = "Time", ylab = "Value")

# 2. Paint additional layers (Low-level)
lines(x, y2, col = "red", lwd = 2)
points(x, y2, pch = 16, col = "red") # Add dots to the red line
abline(h = 50, lty = 2, col = "gray") # Add a dashed horizontal threshold line

I personally find this approach incredibly fast for exploratory data analysis. You don't have to worry about data frames or mapping aesthetics; you just tell R, "Put a line here, put a point there."

Polishing the Visuals without ggplot2

Base R plotting is often criticized for looking "dated," but that's usually because people rely on the defaults. You can make these plots look professional by mastering a few specific arguments that appear across almost all plotting functions:

  • pch (Plotting Character): Changes the point shape. pch = 16 is a solid circle, while pch = 1 is an open circle.
  • lty (Line Type): lty = 1 is solid, lty = 2 is dashed.
  • lwd (Line Width): A numeric value. lwd = 2 makes lines thicker and easier to see.
  • col (Color): Accepts names like "steelblue" or hex codes.

One final tip: if you find that your points are being cut off by the axes, you can use the xlim and ylim arguments inside the initial plot() call to manually define the boundaries of your canvas. Once the canvas is drawn, you can't expand it—you'd have to start over with a new plot() call.




📋 Practical Task

Visualizing Stock Volatility vs. Trend

You are analyzing two hypothetical stocks over a 20-day period. Your goal is to create a single visualization that compares their performance while highlighting a specific price target.

Requirements:

  • Create two vectors: days <- 1:20 and stock_a <- c(100, 102, 101, 105, 107, 108, 110, 109, 112, 115, 114, 118, 120, 122, 121, 125, 128, 130, 132, 135) and stock_b <- c(100, 98, 95, 97, 94, 90, 92, 88, 85, 87, 82, 80, 83, 78, 75, 77, 72, 70, 68, 65).
  • Use a high-level plot() function to draw stock_a as a solid blue line. Include the title "Stock Performance Analysis", and label the axes "Day" and "Price".
  • Use a low-level function to add stock_b as a dashed red line.
  • Add a horizontal gray dashed line at the price of 100 to represent the baseline.
  • Add solid black points (circles) only to the stock_b line to emphasize its daily closing prices.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.