R ggplot2 Line Chart
Line charts show how a value changes over time or across a continuous sequence. They connect data points with lines to make trends, patterns, and turning points visible. ggplot2's geom_line() builds line charts on top of the standard grammar structure.
Basic Line Chart
library(ggplot2)
monthly <- data.frame(
month = 1:12,
revenue = c(42,38,51,55,60,72,68,74,80,78,85,92)
)
ggplot(monthly, aes(x=month, y=revenue)) +
geom_line(color="steelblue", linewidth=1.2) +
scale_x_continuous(breaks=1:12, labels=month.abb) +
labs(title="Monthly Revenue 2024",
x="Month", y="Revenue (₹ thousands)") +
theme_minimal()
Line + Points (geom_point)
ggplot(monthly, aes(x=month, y=revenue)) + geom_line(color="steelblue", linewidth=1) + geom_point(color="steelblue", size=3, shape=21, fill="white") + labs(title="Monthly Revenue with Data Points") + theme_minimal()
Multiple Lines (One per Group)
products <- data.frame(
month = rep(1:6, 2),
product = rep(c("Laptop","Phone"), each=6),
sales = c(12,15,14,18,16,20, 45,48,42,55,60,52)
)
ggplot(products, aes(x=month, y=sales, color=product, group=product)) +
geom_line(linewidth=1.2) +
geom_point(size=2.5) +
scale_color_manual(values=c("Laptop"="steelblue","Phone"="tomato")) +
scale_x_continuous(breaks=1:6, labels=c("Jan","Feb","Mar","Apr","May","Jun")) +
labs(title="Product Sales Jan–Jun",
x="Month", y="Units Sold", color="Product") +
theme_minimal()
Diagram — multiple lines by group:
Sales
│ ╱ Phone
│ ╱
│ ╱ Laptop
│─────────────── Month
Shaded Area Under Line (geom_area)
ggplot(monthly, aes(x=month, y=revenue)) + geom_area(fill="steelblue", alpha=0.3) + geom_line(color="steelblue", linewidth=1.2) + labs(title="Revenue Area Chart") + theme_minimal()
Ribbon — Confidence Band (geom_ribbon)
forecast <- data.frame( month = 1:6, actual = c(60,65,70,68,75,80), lower = c(55,60,63,62,68,73), upper = c(65,70,77,74,82,87) ) ggplot(forecast, aes(x=month)) + geom_ribbon(aes(ymin=lower, ymax=upper), fill="steelblue", alpha=0.2) + geom_line(aes(y=actual), color="steelblue", linewidth=1.2) + labs(title="Revenue Forecast with Confidence Band") + theme_minimal()
Adding a Trend Line
ggplot(monthly, aes(x=month, y=revenue)) + geom_line(color="gray60") + geom_smooth(method="lm", color="red", se=TRUE) + # linear trend labs(title="Revenue Trend (Linear)") + theme_minimal()
Annotating Key Events
ggplot(monthly, aes(x=month, y=revenue)) +
geom_line(color="steelblue", linewidth=1.2) +
geom_vline(xintercept=6, linetype="dashed", color="red") +
annotate("text", x=6.2, y=50, label="New campaign launched",
hjust=0, size=3, color="red") +
scale_x_continuous(breaks=1:12, labels=month.abb) +
labs(title="Revenue with Event Annotation") +
theme_minimal()
Line Type Options
linetype="solid" (1) ─────────── linetype="dashed" (2) ─ ─ ─ ─ ─ linetype="dotted" (3) ········· linetype="dotdash" (4) ─·─·─·─· linetype="longdash" (5) ── ── ──
Line charts are the natural choice whenever you track anything over time — sales trends, website traffic, temperature readings, or stock prices. Always label your axes with time units, mark major events with annotations, and use one clear color per line when showing multiple series.
