🎨 Base R Visualization: Plot Customization
The Artist’s Canvas: Your First Masterpiece
Imagine you’re an artist. You have a blank canvas (your plot) and a box of art supplies (R’s customization tools). Just like a painter adds colors, titles, and fine details to transform a sketch into a masterpiece, you’ll learn to transform basic R plots into stunning visualizations!
🖼️ Plot Parameters: Setting Up Your Canvas
Think of plot parameters like choosing the right canvas size and frame before you start painting. These are the basic settings that control how your entire plot looks.
The Big Three Parameters
# Like choosing canvas size!
plot(1:10, 1:10,
pch = 16, # Point shape
cex = 2, # Point size
lwd = 3) # Line width
| Parameter | What It Does | Example |
|---|---|---|
pch |
Point shape (1-25) | pch = 19 (filled circle) |
cex |
Size multiplier | cex = 2 (double size) |
lwd |
Line thickness | lwd = 3 (thick lines) |
Point Shapes: Your Stamp Collection
# Different stamps for different data!
plot(1:5, pch = c(1, 16, 17, 18, 19))
- 1-14: Open shapes (just outlines)
- 15-20: Filled solid shapes
- 21-25: Filled shapes with borders
💡 Pro tip: Use pch = 19 for clean, filled circles—they look great!
📝 Plot Titles and Labels: Naming Your Art
Every painting needs a title! Labels tell people what they’re looking at.
Adding Titles and Labels
plot(cars$speed, cars$dist,
main = "Speed vs Distance",
xlab = "Speed (mph)",
ylab = "Distance (ft)",
sub = "Car stopping data")
| Argument | Purpose |
|---|---|
main |
Big title at top |
xlab |
Label for x-axis |
ylab |
Label for y-axis |
sub |
Small subtitle below |
Making Titles Pretty
plot(1:10, 1:10,
main = "My Cool Plot",
cex.main = 1.5, # Title size
col.main = "navy", # Title color
font.main = 2) # Bold title
Font styles:
1= Normal2= Bold3= Italic4= Bold + Italic
📏 Plot Axes Customization: Building the Frame
Axes are like the rulers on your canvas—they help viewers understand the scale.
Controlling Axis Limits
# Set your own boundaries
plot(1:10, 1:10,
xlim = c(0, 15), # X goes 0 to 15
ylim = c(-5, 20)) # Y goes -5 to 20
Customizing Axis Appearance
# Remove default axes first
plot(1:10, 1:10, axes = FALSE)
# Add custom axes
axis(1, at = c(2, 5, 8),
labels = c("Low", "Mid", "High"))
axis(2, las = 1) # Horizontal labels
box() # Add the frame back
| Axis Number | Position |
|---|---|
| 1 | Bottom |
| 2 | Left |
| 3 | Top |
| 4 | Right |
The las Parameter: Label Direction
# las controls label rotation
plot(1:5, las = 0) # Parallel to axis
plot(1:5, las = 1) # Always horizontal
plot(1:5, las = 2) # Perpendicular
plot(1:5, las = 3) # Always vertical
🌈 Plot Colors and Styles: Painting Time!
Now the fun part—adding COLOR to your creation!
Basic Color Usage
# Name colors directly
plot(1:10, col = "red")
# Or use hex codes like web design
plot(1:10, col = "#FF5733")
# Or use RGB
plot(1:10, col = rgb(1, 0, 0))
Coloring Different Elements
plot(1:10, 1:10,
col = "blue", # Point color
bg = "yellow", # Fill (pch 21-25)
col.axis = "gray", # Axis text
col.lab = "navy", # Label color
fg = "darkgray") # Foreground
Line Types: Drawing Styles
plot(1:10, type = "l", lty = 2)
lty |
Style |
|---|---|
| 0 | Blank (no line) |
| 1 | Solid ———— |
| 2 | Dashed - - - - |
| 3 | Dotted ····· |
| 4 | Dot-dash ·-·-· |
| 5 | Long dash — — |
| 6 | Two dash —·— |
🎨 Color Functions: Your Color Toolbox
R gives you special functions to create colors!
rgb() - Mix Your Own
# rgb(red, green, blue, alpha)
# Values from 0 to 1
my_red <- rgb(1, 0, 0) # Pure red
my_transparent <- rgb(1, 0, 0, 0.5) # 50% see-through
col2rgb() - Break Down Colors
col2rgb("purple")
# [,1]
# red 160
# green 32
# blue 240
adjustcolor() - Add Transparency
# Make any color see-through
light_blue <- adjustcolor("blue", alpha = 0.3)
plot(1:10, col = light_blue, pch = 19, cex = 3)
colors() - See All 657 Colors!
# List all built-in color names
head(colors(), 20)
# [1] "white" "aliceblue" "antiquewhite"...
🎭 Color Palettes: Ready-Made Color Sets
Palettes are like pre-mixed paint sets—perfect colors that work together!
Built-in Palette Functions
# Rainbow colors
barplot(1:7, col = rainbow(7))
# Heat colors (yellow to red)
barplot(1:7, col = heat.colors(7))
# Terrain colors (green to brown)
barplot(1:7, col = terrain.colors(7))
# Topographic colors
barplot(1:7, col = topo.colors(7))
# Cool cyan-magenta
barplot(1:7, col = cm.colors(7))
Creating Custom Palettes
# Gradient between two colors
my_palette <- colorRampPalette(
c("white", "steelblue")
)(10)
# Use it!
barplot(1:10, col = my_palette)
The RColorBrewer Package
library(RColorBrewer)
# See all palettes
display.brewer.all()
# Use a palette
plot(1:8, col = brewer.pal(8, "Set2"),
pch = 19, cex = 3)
Palette Types:
- Sequential: Light to dark (for ordered data)
- Diverging: Two extremes with neutral middle
- Qualitative: Distinct colors (for categories)
🚀 Putting It All Together
# A complete, beautiful plot!
plot(mtcars$wt, mtcars$mpg,
# Canvas settings
pch = 19,
cex = 1.5,
# Colors
col = adjustcolor("steelblue", 0.7),
# Labels
main = "Car Weight vs Fuel Efficiency",
xlab = "Weight (1000 lbs)",
ylab = "Miles per Gallon",
# Axes
xlim = c(1, 6),
ylim = c(10, 35),
las = 1,
# Style
cex.main = 1.3,
col.main = "navy",
font.main = 2)
# Add a grid for reference
grid(col = "lightgray", lty = 2)
🎯 Quick Reference
graph LR A["plot function"] --> B["Parameters"] A --> C["Labels"] A --> D["Axes"] A --> E["Colors"] B --> B1["pch: shape"] B --> B2["cex: size"] B --> B3["lwd: line width"] C --> C1["main: title"] C --> C2["xlab/ylab: axis labels"] C --> C3["sub: subtitle"] D --> D1["xlim/ylim: limits"] D --> D2["las: label direction"] D --> D3["axis function"] E --> E1["col: colors"] E --> E2["rgb/adjustcolor"] E --> E3["Palettes"]
💪 You Did It!
You now have the power to transform any basic R plot into a beautiful, professional visualization. Remember:
- Parameters set up your canvas
- Titles/Labels tell the story
- Axes frame your data
- Colors bring it to life
- Palettes ensure harmony
Keep experimenting—every great artist started with simple strokes! 🎨
