01 / 22

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.

R Programming

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

Scatter
Shows relationship between two continuous variables
Line
Tracks change in a variable over ordered time
Bar
Compares counts or values across categories
Histogram
Displays the distribution of a single continuous variable
Boxplot
Summarizes spread, median, quartiles, and outliers

Data Types Drive Plot Choice

Before writing any code, ask: what kind of variables do I have? The answer determines which plot to use.

How many variables? One Variable What type? Continuous Histogram (shows distribution) Categorical Bar Plot (shows counts) Two Variables What types? 2 Continuous Scatter Plot (shows relationship) (or Line Plot if ordered) 1 Cont, 1 Cat Boxplot (compares groups)

The Core Syntax Pattern

Every R base plot follows a consistent pattern: call a function, pass your data, then customize with optional arguments.

plot_function(data, main = "Title", xlab = "X Axis", ylab = "Y Axis", col = "color" )

Scatter Plot: Syntax & Anatomy

plot(x = mtcars$wt, y = mtcars$mpg, main = "Weight vs MPG", xlab = "Weight (1000 lbs)", ylab = "Miles per Gallon", pch = 19, col = "steelblue")
10 20 30 40 1 2 3 4 5 Weight (1000 lbs) Miles per Gallon Weight vs MPG

Line Plot: Tracking Trends

months <- 1:12 sales <- c(20,35,45,50,65,80,78,90,85,95,110,120) plot(months, sales, type = "l", lwd = 2, col = "#4ade80", main = "Monthly Sales")
Month Sales Monthly Sales

Bar Plot: Comparing Categories

counts <- c(25, 40, 15, 30) names(counts) <- c("A", "B", "C", "D") barplot(counts, col = "#fbbf24", main = "Category Counts", ylab = "Count")
Category Counts A B C D 25 40 15 30

Histogram: Distribution Shape

hist(mtcars$mpg, breaks = 10, col = "#f472b6", main = "Distribution of MPG", xlab = "Miles per Gallon")
Distribution of MPG Miles per Gallon Frequency 10 15 20 25 30

Boxplot: Five-Number Summary

boxplot(mpg ~ cyl, data = mtcars, col = c("#818cf8", "#4ade80", "#f87171"), main = "MPG by Cylinder Count")
MPG by Cylinder Count 4 cyl 6 cyl 8 cyl 35 25 15 10

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

1
Identify your variables. How many? Continuous or categorical?
2
Choose the right plot type using the decision flowchart from Slide 3.
3
Write the function call with your data as the first argument.
4
Add labels: main, xlab, ylab. Every published plot needs a clear title and labeled axes.
5
Customize appearance: col, pch, lwd, breaks. Adjust for clarity and readability.

Example: Enhanced Scatter Plot

# Scatter with trend line and grid plot(iris$Petal.Length, iris$Petal.Width, pch = 19, col = c("#818cf8","#4ade80","#fbbf24")[iris$Species], main = "Iris Petal Dimensions", xlab = "Petal Length (cm)", ylab = "Petal Width (cm)") # Add a regression line abline(lm(Petal.Width ~ Petal.Length, data = iris), col = "#f87171", lwd = 2) # Add legend legend("topleft", legend = levels(iris$Species), col = c("#818cf8","#4ade80","#fbbf24"), pch = 19)

Example: Customized Boxplot

# Boxplot with formula notation boxplot(Sepal.Width ~ Species, data = iris, col = c("#818cf8","#4ade80","#fbbf24"), border = "#e2e8f0", main = "Sepal Width by Species", notch = TRUE, outline = TRUE)

Example: Multi-Plot Layout

# Create a 2x2 grid of plots par(mfrow = c(2, 2)) plot(mtcars$wt, mtcars$mpg, main = "Scatter", pch = 19) hist(mtcars$mpg, main = "Histogram", col = "#f472b6") barplot(table(mtcars$cyl), main = "Bar", col = "#fbbf24") boxplot(mpg ~ cyl, data = mtcars, main = "Boxplot") par(mfrow = c(1, 1)) # Reset layout

Common Mistakes to Avoid

✕ Wrong Plot Type
Using barplot() on continuous data or hist() on categorical data. Always match the plot to your data type.
✕ Missing Labels
Leaving axes unlabeled or using default "x" and "y." Every plot needs main, xlab, and ylab arguments.
✕ Forgetting table()
Passing raw data to barplot() instead of frequency counts. Use table() first to count categories.
✕ Not Resetting par()
After using par(mfrow), forgetting to reset to c(1,1). All subsequent plots appear tiny in the old grid.

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?