🎨 Base R Visualization: Chart Types
The Art Gallery Analogy
Imagine you’re a museum curator. You have amazing stories to tell, but you need to pick the right frame for each painting. Some stories need a line to show a journey. Others need bars to compare heroes. Some need dots scattered like stars to find hidden patterns.
R’s plot functions are your frames. Each chart type tells your data’s story in a different way. Let’s explore your gallery!
🖼️ Plot Function Basics
The Magic Paintbrush: plot()
Think of plot() like a magic crayon that can draw almost anything. You give it numbers, and it creates pictures!
The simplest drawing:
# Just give it some numbers
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
plot(x, y)
What happens? R looks at your numbers and picks the best picture type automatically!
The Three Magic Words
Every plot understands these basic commands:
| Command | What It Does | Like… |
|---|---|---|
main |
Adds a title | The painting’s name |
xlab |
Labels x-axis | What’s going sideways |
ylab |
Labels y-axis | What’s going up |
Example:
plot(x, y,
main = "My First Chart",
xlab = "Days",
ylab = "Cookies Eaten")
Quick Tips 🚀
- No data?
plot()will show an error - One number list? It uses position as x
- Two lists? First is x, second is y
📈 Line Plots
The Story of a Journey
A line plot is like connecting dots on a treasure map. It shows how something changes over time or moves from one point to another.
When to use it?
- Temperature changes during the day
- Your height as you grow up
- Stock prices over months
Creating Line Plots
Method 1: Using type = "l"
days <- 1:7
temperature <- c(20, 22, 19, 25, 28, 26, 24)
plot(days, temperature,
type = "l",
main = "Week's Temperature",
xlab = "Day",
ylab = "Temp (°C)")
Method 2: The lines() function
# First create an empty plot
plot(days, temperature, type = "n")
# Then add the line
lines(days, temperature, col = "blue")
Line Styles You Can Use
type |
What You Get |
|---|---|
"l" |
Just lines |
"p" |
Just points |
"b" |
Both lines AND points |
"o" |
Points ON the line |
"s" |
Stair steps |
Try this:
plot(1:5, c(1,3,2,5,4),
type = "b",
col = "red",
lwd = 2) # lwd = line width
🔵 Scatter Plots
The Star Map
Imagine throwing a handful of stars onto black paper. Where each star lands tells you something. Scatter plots show relationships between two things - like friends standing together based on how much they like pizza AND ice cream!
When to use it?
- Height vs. Weight
- Study hours vs. Test scores
- Age vs. Shoe size
Creating Scatter Plots
height <- c(150, 160, 155, 170, 165)
weight <- c(50, 65, 55, 75, 60)
plot(height, weight,
main = "Height vs Weight",
xlab = "Height (cm)",
ylab = "Weight (kg)",
pch = 19, # Solid circles
col = "blue")
Point Shapes (pch)
The pch argument changes your dot shapes:
| pch | Shape |
|---|---|
| 1 | ○ Empty circle |
| 16 | ● Solid circle |
| 17 | ▲ Solid triangle |
| 18 | ◆ Solid diamond |
| 19 | ● Larger solid circle |
Pro tip: Use cex to change point size:
plot(x, y, pch = 19, cex = 2) # 2x bigger
Reading Scatter Patterns
graph TD A["Points going up-right?"] --> B["Positive relationship!"] C["Points going down-right?"] --> D["Negative relationship!"] E["Points everywhere?"] --> F["No relationship"]
📊 Bar Plots
The Podium of Champions
Bar plots are like the Olympic podium - they compare things side by side! Each bar’s height shows how big or important something is.
When to use it?
- Comparing scores
- Counting categories (apples vs oranges)
- Showing survey results
Creating Bar Plots
fruits <- c(5, 8, 3, 10)
names(fruits) <- c("Apple", "Banana",
"Cherry", "Date")
barplot(fruits,
main = "Fruit Count",
xlab = "Fruit",
ylab = "Count",
col = "skyblue")
Horizontal Bars
Sometimes it’s easier to read sideways:
barplot(fruits,
horiz = TRUE,
main = "Fruit Count",
col = rainbow(4))
Side-by-Side Bars
Compare groups with matrices:
data <- matrix(c(5,3,8,6,4,7),
nrow = 2)
colnames(data) <- c("Mon","Tue","Wed")
rownames(data) <- c("Boys","Girls")
barplot(data,
beside = TRUE,
col = c("blue","pink"),
legend = rownames(data))
📉 Histograms
The Sorting Hat
A histogram is like sorting students into houses based on their height. It counts how many fall into each “bin” (range).
Key difference from bar plots:
- Bar plots: Compare categories (apples, bananas)
- Histograms: Show distribution of numbers (how many people are 150-160cm tall?)
Creating Histograms
ages <- c(12,15,13,18,14,16,15,17,
14,13,16,15,14,17,16)
hist(ages,
main = "Age Distribution",
xlab = "Age",
ylab = "Frequency",
col = "lightgreen",
border = "darkgreen")
Controlling Bins
# More bins = more detail
hist(ages, breaks = 10)
# Specific breakpoints
hist(ages, breaks = c(12,14,16,18))
What Histograms Tell You
graph TD A["Shape tells the story"] A --> B["Bell shape = Normal"] A --> C["Leaning left = Right-skewed"] A --> D["Leaning right = Left-skewed"] A --> E["Two humps = Bimodal"]
📦 Box Plots
The Five-Number Summary Portrait
A box plot is like a magic portrait that shows 5 important facts about your data in one picture:
- Minimum - The smallest (excluding outliers)
- Q1 - 25% of data is below this
- Median - The middle value
- Q3 - 75% of data is below this
- Maximum - The largest (excluding outliers)
Creating Box Plots
scores <- c(65, 70, 75, 80, 85,
90, 95, 40, 100)
boxplot(scores,
main = "Test Scores",
ylab = "Score",
col = "gold")
Comparing Groups
# Multiple box plots side by side
class_A <- c(75, 80, 82, 78, 85)
class_B <- c(60, 65, 70, 68, 72)
class_C <- c(90, 92, 88, 95, 91)
boxplot(class_A, class_B, class_C,
names = c("A", "B", "C"),
main = "Class Comparison",
col = c("red","green","blue"))
Reading Box Plots
| Part | What It Shows |
|---|---|
| Box | Middle 50% of data |
| Line inside | Median (middle) |
| Whiskers | Range of most data |
| Dots outside | Outliers (unusual values!) |
📐 QQ Plots
The Normal Detector
Q-Q stands for Quantile-Quantile. Think of it as a detective tool that checks if your data follows the “normal” bell curve pattern.
Why does this matter? Many statistics only work correctly if your data is “normal” (follows a bell curve). QQ plots help you check!
Creating QQ Plots
# Generate some data
my_data <- rnorm(100) # Normal data
# Create QQ plot
qqnorm(my_data,
main = "Normal QQ Plot")
qqline(my_data, col = "red")
Reading QQ Plots
The Rule: If points follow the red line = Normal! ✅
graph TD A["Look at the points"] A --> B["Follow the line?"] B --> C["YES = Normal data ✓"] B --> D["NO = Not normal ✗"] D --> E["Curved at ends = Skewed"] D --> F["S-shaped = Heavy tails"]
Example: Normal vs Non-Normal
# Normal data - points on line
normal <- rnorm(100)
qqnorm(normal, main = "Normal")
qqline(normal)
# Skewed data - points curve away
skewed <- rexp(100) # Exponential
qqnorm(skewed, main = "Skewed")
qqline(skewed)
🎯 Quick Reference: Which Chart When?
| Your Goal | Use This |
|---|---|
| Show change over time | Line Plot |
| Find relationships | Scatter Plot |
| Compare categories | Bar Plot |
| Show distribution | Histogram |
| Summarize with 5 numbers | Box Plot |
| Check if data is normal | QQ Plot |
🌟 Your First Gallery
You now have six powerful frames for your data stories:
- Line plots - The journey teller
- Scatter plots - The relationship finder
- Bar plots - The champion comparer
- Histograms - The sorting hat
- Box plots - The five-fact portrait
- QQ plots - The normal detector
Each one has a special purpose. Choose wisely, and your data will speak clearly to everyone who looks at your charts!
Remember: The best chart is the one that tells your story simply and honestly. 🎨
