Creating Common
Plots in R
Master the five essential plot types: scatter, line, bar, histogram, and boxplot. Learn when and how to use each one to visualize data effectively.
Why Visualize Data?
Raw numbers rarely tell the full story. A well-chosen plot can reveal patterns, outliers, and distributions that would be invisible in a spreadsheet. R's built-in plot() family makes this effortless.
The Five Essential Plot Types
Data Types Drive Plot Choice
Before writing any code, ask: what kind of variables do I have? The answer determines which plot to use.
The Core Syntax Pattern
Every R base plot follows a consistent pattern: call a function, pass your data, then customize with optional arguments.
Scatter Plot: Syntax & Anatomy
Line Plot: Tracking Trends
Bar Plot: Comparing Categories
Histogram: Distribution Shape
Boxplot: Five-Number Summary
Histogram vs Bar Plot
These two look similar but serve fundamentally different purposes. Confusing them is the most common beginner mistake.
| Histogram | Bar Plot | |
|---|---|---|
| Data type | Continuous (numeric) | Categorical (groups) |
| Function | hist() | barplot() |
| Bars touch? | Yes: continuous bins | No: separate groups |
| X-axis | Numeric ranges | Category labels |
| Shows | Distribution shape | Comparison of values |
How to Build Any Plot
Example: Enhanced Scatter Plot
Example: Customized Boxplot
Example: Multi-Plot Layout
Common Mistakes to Avoid
Quick Reference Table
| Plot | Function | Key Arg | Use When |
|---|---|---|---|
| Scatter | plot(x, y) | pch, col | 2 continuous vars |
| Line | plot(x, y, type="l") | type, lwd | Trends over time |
| Bar | barplot(counts) | names.arg | Categorical data |
| Histogram | hist(x) | breaks, col | 1 continuous var |
| Boxplot | boxplot(y ~ x) | notch | Compare distributions |
Question 1 of 5
Which R function creates a scatter plot of two numeric vectors?
Question 2 of 5
To create a line plot instead of a scatter plot, which argument do you add to the plot() function?
Question 3 of 5
You have a vector of student test scores and want to see the shape of the distribution. Which plot should you use?
Question 4 of 5
What does the tilde (~) mean in the boxplot call boxplot(mpg ~ cyl, data = mtcars)?
Question 5 of 5
Before passing categorical data to barplot(), you typically need to wrap it in which function?