0
0
R Programmingprogramming~5 mins

Line plots (geom_line) in R Programming

Choose your learning style9 modes available
Introduction

Line plots help you see how values change over time or order. They connect points with lines to show trends clearly.

You want to show how sales change each month.
You want to track temperature changes during a day.
You want to compare growth of two plants over weeks.
You want to visualize stock prices over days.
Syntax
R Programming
ggplot(data, aes(x = x_variable, y = y_variable)) +
  geom_line()

data is your dataset.

aes() sets which columns go on x and y axes.

Examples
Basic line plot with time on x-axis and value on y-axis.
R Programming
ggplot(df, aes(x = time, y = value)) +
  geom_line()
Line plot with different colors for each city.
R Programming
ggplot(df, aes(x = day, y = temperature, color = city)) +
  geom_line()
Line plot with thicker dashed lines.
R Programming
ggplot(df, aes(x = date, y = sales)) +
  geom_line(size = 2, linetype = "dashed")
Sample Program

This program makes a simple line plot showing sales over 6 months. The line connects sales points month by month.

R Programming
library(ggplot2)

# Create example data
sales_data <- data.frame(
  month = 1:6,
  sales = c(100, 120, 150, 130, 170, 200)
)

# Make line plot
plot <- ggplot(sales_data, aes(x = month, y = sales)) +
  geom_line() +
  labs(title = "Monthly Sales", x = "Month", y = "Sales")

print(plot)
OutputSuccess
Important Notes

Make sure your x-axis data is ordered correctly for the line to make sense.

You can add colors or styles to lines to compare groups.

Use geom_point() with geom_line() to show points on the line.

Summary

Line plots connect points to show trends over time or order.

Use geom_line() inside ggplot() to create line plots.

Customize lines with colors, sizes, and types to make your plot clearer.