Chapter 37: R Line
R Line plots — that is, how to draw lines in plots.
In R there are two main ways to create line plots:
- Base R → very fast, built-in, good for quick checks
- ggplot2 → modern, beautiful, customizable, used in almost all published work in 2026
I’ll explain both in detail, like we’re sitting together looking at the same screen, with lots of copy-paste examples you can run right now.
1. What is a “Line” in R Plotting?
A line plot connects data points with straight lines (or sometimes smooth curves). Typical uses:
- Time series (temperature over days, stock prices over months)
- Trends (growth of sales, learning curve)
- Function plots (y = x², sine wave)
- Before-after comparisons
2. Base R Line Plots – Quick & Simple
Base R uses the plot() function with type = “l” (for line) or type = “o” (overplotted points + lines).
Example 1 – Simple daily temperature line (Hyderabad style)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Fake data – one week in Feb 2026 days <- 1:7 temps <- c(28.2, 29.5, 30.8, 31.4, 30.1, 29.0, 28.7) plot(days, temps, type = "l", # "l" = line only col = "darkred", # line color lwd = 2.5, # line width main = "Hyderabad Daily Temperature – Feb 2026", xlab = "Day of Week", ylab = "Temperature (°C)", ylim = c(27, 32), xaxt = "n") # hide default x-axis # Add nice x-axis labels axis(1, at = days, labels = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")) # Add points on top (common style) points(days, temps, pch = 19, col = "darkred", cex = 1.4) # Add horizontal reference line abline(h = 30, col = "blue", lty = 2, lwd = 1.5) # dashed line at 30°C |
Example 2 – Multiple lines (compare two areas)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Two areas gachibowli <- c(29.1, 30.4, 31.7, 32.5, 31.2, 30.0, 29.3) hitech <- c(28.5, 29.0, 29.8, 30.5, 30.1, 29.4, 28.8) plot(days, gachibowli, type = "l", col = "darkorange", lwd = 2.5, main = "Temperature Comparison – Gachibowli vs Hitech City", xlab = "Day", ylab = "°C", ylim = c(27, 33)) lines(days, hitech, col = "darkblue", lwd = 2.5) legend("topright", legend = c("Gachibowli", "Hitech City"), col = c("darkorange", "darkblue"), lwd = 2.5, bty = "n") |
3. ggplot2 Line Plots – The Modern Professional Way
ggplot2 makes lines look clean and publication-ready very easily.
Same temperature example – but prettier
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
library(ggplot2) # Prepare data in long format (best for ggplot) library(tidyr) # for pivot_longer weather <- data.frame( day = rep(1:7, 2), area = rep(c("Gachibowli", "Hitech City"), each = 7), temp = c(gachibowli, hitech) ) ggplot(weather, aes(x = day, y = temp, color = area, group = area)) + geom_line(linewidth = 1.4) + # linewidth instead of size in newer versions geom_point(size = 3, shape = 21, fill = "white") + # white-filled points on line scale_color_manual(values = c("darkorange", "darkblue")) + scale_x_continuous(breaks = 1:7, labels = c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")) + labs( title = "Daily Temperature Comparison", subtitle = "Gachibowli vs Hitech City – Feb 2026", x = NULL, y = "Temperature (°C)", color = "Area" ) + theme_minimal(base_size = 14) + theme(legend.position = "bottom", plot.title = element_text(face = "bold")) |
Add confidence ribbon (common for forecasts / means)
|
0 1 2 3 4 5 6 7 8 9 10 |
ggplot(weather, aes(day, temp, color = area, fill = area)) + geom_line(linewidth = 1.2) + geom_ribbon(aes(ymin = temp - 0.8, ymax = temp + 0.8), alpha = 0.15) + theme_light() + labs(title = "Temperature with Uncertainty Band") |
4. Quick Comparison Table (2026 View)
| Feature | Base R plot(type=”l”) | ggplot2 geom_line() | Winner for most people |
|---|---|---|---|
| Speed | Extremely fast | Slightly slower (but < 0.5 sec usually) | Base |
| Multiple lines | lines() manually | Automatic via color = or group = | ggplot2 |
| Legend | Manual legend() | Automatic | ggplot2 |
| Themes / look | Classic / old-school | Modern, many built-in themes | ggplot2 |
| Facets / small multiples | Hard / manual | facet_wrap() – one line | ggplot2 |
| Saving high-res | png(), pdf() | ggsave(dpi = 300) | Both fine |
| Interactive (Shiny / html) | No | Easy with ggplotly() | ggplot2 |
5. Your Mini Practice Right Now (Copy → Run!)
Try this block — then change colors, add points, try geom_step() or geom_smooth():
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
library(ggplot2) # Fake Hyderabad traffic speed over hours hours <- 8:20 speed_gachibowli <- c(45, 48, 35, 22, 18, 25, 38, 55, 62, 58, 50, 42, 38) speed_hitech <- c(50, 52, 40, 28, 20, 30, 45, 60, 68, 65, 55, 48, 40) df <- data.frame( hour = rep(hours, 2), area = rep(c("Gachibowli", "Hitech City"), each = length(hours)), speed_kmh = c(speed_gachibowli, speed_hitech) ) ggplot(df, aes(hour, speed_kmh, color = area)) + geom_line(linewidth = 1.5) + geom_point(size = 2.8, shape = 21, fill = "white") + scale_x_continuous(breaks = seq(8, 20, by = 2)) + scale_color_manual(values = c("#E69F00", "#56B4E9")) + labs( title = "Traffic Speed During Day", subtitle = "Gachibowli vs Hitech City – Feb 2026", x = "Hour of Day", y = "Average Speed (km/h)" ) + theme_minimal(base_size = 14) + theme(legend.position = "top") |
Now try:
- Change to geom_step() (staircase line)
- Add geom_smooth() with se = FALSE
- Use facet_wrap(~ area)
Which style do you like more — base or ggplot?
Want to go deeper?
- Time-series specific tricks (date axes, scale_x_date())
- Adding annotations / arrows / text
- Saving publication-ready line plots (PDF, high DPI)
- Or next topic (bar plots, histograms, heatmaps)?
Just tell me — whiteboard is still clean! 📈☕🚀
