Write R code to create a line plot using the vectors month <- 1:6 and sales <- c(200, 250, 300, 280, 320, 400). Use base R’s plot() function to create a line plot titled "Monthly Sales" where the xlab = "Month", and the ylab = "Sales". Make the line "Green" with the lwd = "2"
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
.
# 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)
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.