How to Use geom_line in ggplot2 for Line Plots in R
Use
geom_line() in ggplot2 to add lines connecting data points in a plot. It requires mapping x and y aesthetics to your data, typically inside ggplot(). This creates a line plot showing trends or changes over a continuous variable.Syntax
The basic syntax of geom_line() is used inside a ggplot() call. You map your data's variables to x and y aesthetics to tell ggplot what to plot on each axis. Additional options can customize the line's appearance.
- ggplot(data, aes(x, y)): Sets the data and maps x and y variables.
- geom_line(): Adds lines connecting points in order of x.
- color, size, linetype: Optional arguments to style the line.
r
ggplot(data, aes(x = x_variable, y = y_variable)) + geom_line(color = "blue", size = 1)
Example
This example shows how to create a simple line plot of the pressure dataset included in R. It plots temperature on the x-axis and pressure on the y-axis, connecting points with a blue line.
r
library(ggplot2) ggplot(pressure, aes(x = temperature, y = pressure)) + geom_line(color = "blue", size = 1) + labs(title = "Pressure vs Temperature", x = "Temperature (degrees Celsius)", y = "Pressure (mm of mercury)")
Output
A line plot with temperature on the horizontal axis and pressure on the vertical axis. A smooth blue line connects the data points showing how pressure changes with temperature.
Common Pitfalls
Common mistakes when using geom_line() include:
- Not mapping
xandyaesthetics correctly, which causes errors or empty plots. - Using unordered or categorical x-values without grouping, which can produce unexpected lines.
- For grouped data, forgetting to specify
groupaesthetic, causing lines to connect wrong points.
Example of a common mistake and fix:
r
# Wrong: No group aesthetic for multiple lines library(ggplot2) data <- data.frame( time = rep(1:3, 2), value = c(2, 3, 5, 4, 6, 7), group = rep(c("A", "B"), each = 3) ) # This will connect all points in one line wrong_plot <- ggplot(data, aes(x = time, y = value)) + geom_line() # Correct: Add group aesthetic to separate lines correct_plot <- ggplot(data, aes(x = time, y = value, group = group, color = group)) + geom_line() wrong_plot correct_plot
Output
The first plot draws one line connecting all points ignoring groups, resulting in a zigzag line. The second plot draws two separate lines, one for each group, correctly showing grouped trends.
Quick Reference
Here is a quick summary of key geom_line() options:
| Option | Description | Example |
|---|---|---|
| aes(x, y) | Map x and y variables to axes | aes(x = time, y = value) |
| color | Set line color | geom_line(color = "red") |
| size | Set line thickness | geom_line(size = 1.5) |
| linetype | Set line style (solid, dashed, etc.) | geom_line(linetype = "dashed") |
| group | Group data for multiple lines | aes(group = category) |
Key Takeaways
Use geom_line() inside ggplot() with x and y aesthetics to create line plots.
Always map x and y variables correctly to avoid empty or incorrect plots.
For multiple groups, specify the group aesthetic to draw separate lines.
Customize line appearance with color, size, and linetype arguments.
Check data order on x-axis to ensure lines connect points as expected.