Server Info & Course Info
png("my_plot.png")
# or
pdf("my_plot.pdf")
plot(1,1)
plot(c(1,2),c(2,3))
boxplot(rnorm(10),rnorm(10))
# Save
dev.off()
plot(rnorm(100)) # Automatically displays in RStudio
pdf("output.pdf") # Start PDF device
hist(mtcars$mpg) # Draw plot
dev.off() # Save file
col)plot(1:5, col=c("red","green","blue","cyan","magenta"), pch=16, cex=3)
col="#FF0000" for pure redpch)# Plot different point styles
plot(1:6, rep(1,6), pch=16:21, col=rainbow(6), cex=3)
lty)# Create empty plot
plot(1, type="n", xlim=c(0,6), ylim=c(0,7))
# Draw different lines
for(i in 1:6) {
abline(h=i, lty=i-1, lwd=2)
}
plot(mtcars$wt, mtcars$mpg,
col=ifelse(mtcars$am==1, "red", "blue"),
pch=ifelse(mtcars$cyl==4, 16, 17),
lty=2)

plot()type parameterplot()?# Minimal example
plot(1:5)
Create arithmetic sequence with : operator:
x <- 1:10
y <- 1:10
plot(x, y)
Use y ~ x syntax with datasets:
# Using built-in mtcars data
plot(mpg ~ wt, data = mtcars)
type ParameterControls how points are displayed:
| Code | Meaning |
|---|---|
| “p” | Points |
| “l” | Lines |
| “b” | Both points and lines |
x <- 1:10
y <- rnorm(10) # Random numbers
plot(x, y, type = "p")
Connect points with lines:
x <- seq(0, 2*pi, length=50)
y <- sin(x)
plot(x, y, type = "l")
Best of both worlds:
x <- c(1,3,5,7,9)
y <- c(2,4,1,8,5)
plot(x, y, type = "b")
Combine parameters for better visualization:
x <- seq(0, 2*pi, length=50)
y <- sin(x)
plot(x, y,
type = "b",
col = "blue",
main = "Sine Wave Demo",
xlab = "Angle (radians)",
ylab = "sin(x)")
You can add these to any plot:
main = "Title" # Add title
col = "red" # Change color
pch = 17 # Change point shape
cex = 2 # Make points bigger
Let’s create together:

# Create base plot
plot(mtcars$mpg, mtcars$hp,
main = "Car Performance",
xlab = "MPG", ylab = "Horsepower",
pch = 16, col = "gray")
points()# Highlight high-performance cars
special_cars <- mtcars[mtcars$hp > 300, ]
points(special_cars$mpg, special_cars$hp,
pch = 17, # Triangle shape
col = "red",
cex = 1.5) # Size
What’s happening:
pch=17: Triangle plotting symbolcol="red": Makes points stand outcex=1.5: 50% larger than defaultlines()# Add linear regression line
model <- lm(hp ~ mpg, data = mtcars) # linear regression will be taught next week
x_values <- seq(10, 35, length.out = 100)
y_values <- predict(model, newdata = data.frame(mpg = x_values))
lines(x_values, y_values,
col = "blue",
lwd = 2, # Line width
lty = 2) # Dashed line
Note: Always calculate prediction values first!
rect()# Highlight unusual MPG range
rect(xleft = 15, ybottom = 50,
xright = 20, ytop = 350,
border = "red",
col = rgb(1, 0, 0, 0.2)) # Transparent red
Parameters:
xleft, xright: X-axis boundariesybottom, ytop: Y-axis boundariesrgb(): Creates transparent colorsegments()# Add mean reference lines to boxplot
boxplot(mpg ~ cyl, data = mtcars, col = "lightblue")
cyl_means <- aggregate(mpg ~ cyl, mtcars, mean) # aggregate function is use to stat X based on Y
segments(x0 = 1:3 - 0.4, y0 = cyl_means$mpg,
x1 = 1:3 + 0.4, y1 = cyl_means$mpg,
col = "darkred", lwd = 3)
Breakdown:
x0/x1: Horizontal line positionsy0/y1: Vertical positions (same for horizontal line)lwd=3: Thick line for visibilitypar(new=TRUE) carefully (not recommended for beginners)Combine all elements we learned:
plot(mtcars$mpg, mtcars$hp)
points(special_cars$mpg, special_cars$hp, col="red")
lines(x_values, y_values, col="blue")
rect(15, 50, 20, 350, border="red")
segments(10, 150, 30, 150, col="green")
Experiment with colors and positions!

A picture that shows patterns in numbers using colors
Example: Gene expression levels (high=red, low=white)
# Create sample data
gene_data <- matrix(rnorm(100, mean=10), nrow=10)
colnames(gene_data) <- paste("Patient", 1:10)
rownames(gene_data) <- paste("Gene", LETTERS[1:10])
# Simple heatmap
heatmap(gene_data)
Remove the tree diagrams:
heatmap(gene_data,
Rowv = NA, # No row clustering
Colv = NA) # No column clustering
Shows data distribution through 5 numbers:
📦 Minimum, Q1, Median, Q3, Maximum
Perfect for comparing groups!
# Use built-in iris data
boxplot(Petal.Length ~ Species,
data = iris,
col = c("pink", "lightblue", "yellowgreen"))
Add colors and labels:
boxplot(Petal.Length ~ Species,
data = iris,
main = "Petal Length Comparison",
xlab = "Iris Species",
ylab = "Length (cm)",
col = heat.colors(3))
Heatmaps help see:
🧬 Gene expression patterns
🦠 Microbial communities
Boxplots help compare:
🌷 Flower measurements
🏥 Medical test results
Experiment with colors in heatmaps:
heatmap(gene_data, col = terrain.colors(50))
Change boxplot orientation:
boxplot(Petal.Length ~ Species, data=iris, horizontal=TRUE)
Heatmap essentials:
🔥 heatmap() needs a numeric matrix
🎨 Use col= to change colors
Boxplot basics:
📦 Formula syntax: y ~ group
🌈 Colors make comparisons easier

Section 5. Practical Visualization Cases
In biomedical research:
# Good visualization can reveal:
- Dose-response relationships 📈
- Group differences 🧪🔬
- Data quality issues ⚠️
Scenario: Test drug efficacy at different concentrations
# Sample data
doses <- c(0, 10, 25, 50, 100)
response <- c(5, 15, 40, 65, 85)
# Basic plot
plot(doses, response,
type = "b",
main = "Drug Dose-Response Curve",
xlab = "Dose (mg/mL)",
ylab = "Effect (%)",
col = "darkred",
pch = 19)
Add professional touches:
# Add gridlines
grid(col = "lightgray")
# Add confidence intervals
arrows(doses, response-5, doses, response+5,
length=0.05, angle=90, code=3)
Task: Compare cholesterol levels between groups
# Generate sample data
set.seed(123)
control <- rnorm(50, mean = 180, sd = 20)
treatment <- rnorm(50, mean = 160, sd = 15)
# Create boxplot
boxplot(list(Control=control, Treatment=treatment),
col = c("#FFC107", "#2196F3"),
main = "Cholesterol Levels Comparison",
ylab = "mg/dL")
# Key elements:
- Central line: Median
- Box: 25%-75% percentile
- Whiskers: 1.5×IQR
- Outliers: Beyond whiskers ⚠
Best for web/quick sharing
png("my_plot.png", width=800, height=600)
# Plotting commands
plot(doses, response)
dev.off() # IMPORTANT!
🔑 Pro Tip: Always close device with dev.off()!
Best for publications/editing
pdf("figure.pdf",
width=8, # inches
height=6)
# Plotting commands
boxplot(control, treatment)
dev.off()
📄 Advantage: Vector graphics = Infinite zoom!
1. Forgetting dev.off() -> Empty files ❌
2. Wrong plot dimensions -> Cropped labels 📏
3. Saving while plot window is open -> Conflicts 💥
4. Overwriting files -> Use unique names 🏷️
Create a combined plot:
# Set up PDF output
pdf("combined_plots.pdf", width=10, height=5)
# Split screen
par(mfrow=c(1,2))
plot(doses, response, main="Dose Response")
boxplot(list(C=control, T=treatment), main="Cholesterol")
dev.off()