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:

  1. Base R → very fast, built-in, good for quick checks
  2. 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)

R

Example 2 – Multiple lines (compare two areas)

R

3. ggplot2 Line Plots – The Modern Professional Way

ggplot2 makes lines look clean and publication-ready very easily.

Same temperature example – but prettier

R

Add confidence ribbon (common for forecasts / means)

R

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():

R

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! 📈☕🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *