Next →

Output:


      

Plots:

Line Plots

Line plots are useful for visualizing trends over time or ordered data. In R, you can create line plots using the base plot() function or ggplot2.

Basic Line Plot

# Sample data
x <- 1:5
y <- c(2, 4, 3, 5, 7)

# Create a line plot
plot(x, y, type = "l", main = "Basic Line Plot", xlab = "X Values", ylab = "Y Values", col = "blue", lwd = 2)

Using ggplot2

library(ggplot2)

df <- data.frame(x = x, y = y)

ggplot(df, aes(x = x, y = y)) +
  geom_line(color = "red", size = 1) +
  ggtitle("Line Plot with ggplot2") +
  theme_minimal()

Line plots are ideal for showing patterns, trends, or changes in a dataset over an interval.