Back to Course
Visualization

Base R Graphics

Before reaching for ggplot2, every R programmer should understand base R graphics — the built-in plotting system that requires no extra packages and is perfect for quick, exploratory visualizations.

1. The plot() Function

x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)

plot(x, y,
     main = "Simple Line Relationship",
     xlab = "X Values", ylab = "Y Values",
     col = "steelblue", pch = 19, type = "b")
ParameterPurpose
mainChart title
xlab / ylabAxis labels
colColor of points/lines
pchPoint shape (1–25)
type"p" points, "l" lines, "b" both

2. Histograms — Distribution of a Single Variable

scores <- rnorm(200, mean = 70, sd = 10)

hist(scores,
     main = "Distribution of Test Scores",
     xlab = "Score", col = "lightblue",
     breaks = 20, border = "white")

3. Boxplots — Spotting Spread and Outliers

boxplot(scores,
        main = "Score Spread",
        ylab = "Score", col = "orange")

# Boxplot by group
boxplot(salary ~ department, data = df,
        main = "Salary by Department",
        col = c("skyblue", "salmon", "lightgreen"))
Pro Tip: Boxplots visually flag outliers as individual points beyond the 'whiskers' — a fast complement to the IQR calculation from the previous lesson.

4. Bar Charts and Scatter Plots

counts <- table(df$department)
barplot(counts,
        main = "Employees per Department",
        col = "purple", ylab = "Count")

plot(df$age, df$salary,
     main = "Age vs Salary",
     xlab = "Age", ylab = "Salary",
     col = "darkgreen", pch = 16)

5. Combining Multiple Plots

Use par(mfrow = c(rows, cols)) to arrange multiple charts in a grid:

par(mfrow = c(1, 2))   # 1 row, 2 columns
hist(scores, main = "Histogram")
boxplot(scores, main = "Boxplot")
par(mfrow = c(1, 1))   # reset to single plot layout

6. Saving Plots to File

png("output/scores_histogram.png", width = 800, height = 600)
hist(scores, main = "Score Distribution", col = "lightblue")
dev.off()   # important — closes and writes the file
Common Issues: Forgetting dev.off() is one of the most common base R plotting mistakes — the image file will be empty or corrupted without it.

7. Base R vs. ggplot2 — When to Use Which

Use CaseRecommended Tool
Quick, exploratory look at dataBase R (plot, hist, boxplot)
Publication-quality, layered visualsggplot2 (next lesson)
No extra packages availableBase R
Complex faceting, custom themesggplot2

8. Production-Ready Checklist

  • ✅ Master plot(), hist(), boxplot(), barplot() — the core base R chart functions.
  • ✅ Customize with main, xlab, ylab, col — for clear, labeled charts.
  • ✅ Use par(mfrow) for grid layouts — comparing multiple charts at once.
  • ✅ Always call dev.off() — when saving plots to file.
Pro Tip: Base R graphics are perfect for fast, no-dependency exploration during analysis. Save ggplot2 for the polished, final visuals you'll share with others.

Ready to master R Programming?

Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.

Explore Course