Menu

Tutorial7 min read

How to Plot Time Series and Line Graphs in R with ggplot2

By Francesco Villasmunta
How to Plot Time Series and Line Graphs in R with ggplot2

Line plots are the primary tool to show trends over continuous variables, especially time. In R's ggplot2, line plots are built with the geom_line() geometry, often paired with geom_point() to highlight discrete sampling events.

In This Tutorial

0.Live Code: Multiple Series Line Plot

1.Basic Line Plot (geom_line)

2.Handling Date and Time Variables

3.Mapping Colors and Line Types

4.Adding Area Fills

0. Live Code: Multiple Series Line Plot

Time-series rendering. Customize parameters using Python below, or upload your data to run R directly.

1. Basic Line Plot with geom_line

A simple line graph connects discrete coordinate observations chronologically:

R / ggplot2

ggplot(df, aes(x = time_step, y = measurement)) +
  geom_line(color = "#4f46e5", size = 1)

2. Handling Date and Time Variables

Try it

Try it now: plot your own time series

Upload temporal data and generate a clean time-series figure with trend smoothing and readable date formatting.

Generate my time-series chart

Newsletter

Get a weekly Python plotting tip

One concise tip each week for cleaner, faster scientific figures. Built for researchers who publish.

No spam. Unsubscribe anytime.

Ensure date columns are parsed correctly using R's as.Date(). Format scale labels using the `scales` library:

R / ggplot2

library(scales)

df$date <- as.Date(df$date)

ggplot(df, aes(x = date, y = measurement)) +
  geom_line() +
  scale_x_date(date_labels = "%b %Y", date_breaks = "2 months")

3. Mapping Colors and Line Types

Distinguish multiple treatments using combinations of colors and dashed line patterns for accessibility:

R / ggplot2

ggplot(df, aes(x = time_step, y = measurement, color = treatment, linetype = treatment)) +
  geom_line(size = 1.2) +
  scale_color_manual(values = c("#4f46e5", "#059669"))

4. Adding Area Fills (geom_ribbon)

Overlay a shaded margin representation for errors or confidence boundaries:

R / ggplot2

ggplot(df, aes(x = time_step, y = measurement)) +
  geom_ribbon(aes(ymin = measurement - standard_error, ymax = measurement + standard_error), fill = "gray80", alpha = 0.4) +
  geom_line(color = "black", size = 1)

Chart gallery

Explore related formats

Review linear trends.

Browse all chart types →
Multi-line graph showing temperature trends for 3 cities over a year
Time Seriesmatplotlib, seaborn
From the chart galleryStock price tracking over time

Line Graph

Displays data points connected by straight line segments to show trends over time.

Sample code / prompt

import matplotlib.pyplot as plt
import numpy as np

# Generate temperature data for 3 major US cities over 12 months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
nyc = [30, 32, 40, 52, 65, 75, 82, 81, 74, 63, 50, 38]
miami = [65, 66, 70, 76, 82, 87, 90, 90, 87, 80, 72, 66]
chicago = [25, 27, 35, 48, 62, 72, 80, 79, 71, 60, 45, 32]

# Create figure with enhanced styling
Scatter plot of height vs weight colored by gender with regression line
Statisticalmatplotlib, seaborn
From the chart galleryCorrelation analysis between metrics

Scatterplot

Displays values for two variables as points on a Cartesian coordinate system.

Sample code / prompt

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd

# Generate sample data
np.random.seed(42)
n_samples = 200
height = np.random.normal(170, 8, n_samples)
weight = height * 0.6 + np.random.normal(0, 8, n_samples) - 50

Build This Time Series Plot Online

Upload your data and describe the design. Plotivy writes the ggplot2 code and executes it instantly.

Use R Analyzer
Tags:#R#ggplot2#line plot#time series#geom_line

Found this helpful? Share it with your network.

FV
Francesco Villasmunta

Experimental Physicist & Photonics Researcher

Hands-on experience in silicon photonics, semiconductor fabrication (DRIE/ICP-RIE), optical simulation, and data-driven analysis. Built Plotivy to help researchers focus on discoveries instead of data struggles.

More about the author

Visualize your own data

Apply the techniques from this article to your own datasets. Upload CSV, Excel, or paste data directly.

Start Analyzing - Free