R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
22: Base R Plotting
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 = 16is a solid circle, whilepch = 1is an open circle.lty(Line Type):lty = 1is solid,lty = 2is dashed.lwd(Line Width): A numeric value.lwd = 2makes 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:20andstock_a <- c(100, 102, 101, 105, 107, 108, 110, 109, 112, 115, 114, 118, 120, 122, 121, 125, 128, 130, 132, 135)andstock_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 drawstock_aas 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_bas 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_bline to emphasize its daily closing prices.
There are no comments for now.