R Line
Line plots in R are used to display data points connected by straight lines, useful for showing trends over time. The plot() function is used to create a line plot by specifying the type parameter as "l".
Key Topics
Line Plot Example
# Creating a line plot
x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 5, 7, 11)
plot(x, y, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")
Output:
A line plot connecting the points (1, 2), (2, 3), (3, 5), (4, 7), and (5, 11).
Code Explanation: The plot() function creates a line plot with the type parameter set to "l", which stands for line.
Customizing Line Plot
Line plots can be customized by changing colors, line types, and adding points.
# Customizing the line plot
plot(x, y, type = "o", col = "red", lty = 2, pch = 16,
main = "Customized Line Plot", xlab = "X-axis", ylab = "Y-axis")
Output:
A red line plot with dashed lines and filled points.
Code Explanation: The type parameter "o" plots both lines and points. The col parameter sets the color to red, lty specifies a dashed line, and pch changes the point shape.
Key Takeaways
- Use the
plot()function withtype = "l"to create a line plot. - Customizations include colors, line types, and adding points.
- Line plots are ideal for visualizing trends over time.