Introduction
In this chapter, you will learn how to create and customize line graphs in R. Line graphs are useful for displaying trends over time or continuous data. They are particularly effective for showing how a variable changes over time or across different conditions. R provides built-in functions for creating line graphs using base R graphics.
Creating Line Graphs with Base R
The base R plot()
function allows you to create simple line graphs quickly.
Basic Line Graph
To create a basic line graph, you need a vector of x-values and a vector of y-values.
Example:
# Creating a basic line graph
x <- 1:10
y <- x^2
plot(x, y, type = "l", main = "Basic Line Graph", xlab = "X-axis", ylab = "Y-axis")
Adding Points
You can add points to a line graph by setting the type
parameter to "b"
.
Example:
# Adding points to the line graph
plot(x, y, type = "b", main = "Line Graph with Points", xlab = "X-axis", ylab = "Y-axis")
Customizing Line Graphs
You can customize line graphs by adding colors, labels, and other graphical parameters.
Adding Colors and Line Types
Use the col
parameter to specify colors and the lty
parameter to specify line types.
Example:
# Customizing the line graph with colors and line types
plot(x, y, type = "l", col = "blue", lty = 2, main = "Customized Line Graph", xlab = "X-axis", ylab = "Y-axis")
Adding Titles and Axis Labels
You can add main titles, axis labels, and subtitles using the main
, xlab
, ylab
, and sub
parameters.
Example:
# Adding titles and axis labels
plot(x, y, type = "l", main = "Customized Line Graph", xlab = "X-axis", ylab = "Y-axis", sub = "Subtitle")
Adding Legends
Use the legend()
function to add a legend to the line graph.
Example:
# Adding a legend
plot(x, y, type = "l", col = "blue", main = "Line Graph with Legend", xlab = "X-axis", ylab = "Y-axis")
legend("topright", legend = "y = x^2", col = "blue", lty = 1)
Multiple Lines
You can add multiple lines to a single graph by using the lines()
function after the initial plot()
call.
Example:
# Creating multiple lines
y2 <- x^3
plot(x, y, type = "l", col = "blue", ylim = c(0, max(y2)), main = "Line Graph with Multiple Lines", xlab = "X-axis", ylab = "Y-axis")
lines(x, y2, col = "red")
legend("topright", legend = c("y = x^2", "y = x^3"), col = c("blue", "red"), lty = 1)
Example Program Using Line Graphs
Here is an example program that demonstrates the creation and customization of line graphs in R using the base plot()
function.
# R Program to Demonstrate Line Graphs
# Data for the line graph
x <- 1:10
y <- x^2
# Basic line graph
plot(x, y, type = "l", main = "Basic Line Graph", xlab = "X-axis", ylab = "Y-axis")
# Line graph with points
plot(x, y, type = "b", main = "Line Graph with Points", xlab = "X-axis", ylab = "Y-axis")
# Customized line graph with colors and line types
plot(x, y, type = "l", col = "blue", lty = 2, main = "Customized Line Graph", xlab = "X-axis", ylab = "Y-axis")
# Adding titles and axis labels
plot(x, y, type = "l", main = "Customized Line Graph", xlab = "X-axis", ylab = "Y-axis", sub = "Subtitle")
# Line graph with a legend
plot(x, y, type = "l", col = "blue", main = "Line Graph with Legend", xlab = "X-axis", ylab = "Y-axis")
legend("topright", legend = "y = x^2", col = "blue", lty = 1)
# Line graph with multiple lines
y2 <- x^3
plot(x, y, type = "l", col = "blue", ylim = c(0, max(y2)), main = "Line Graph with Multiple Lines", xlab = "X-axis", ylab = "Y-axis")
lines(x, y2, col = "red")
legend("topright", legend = c("y = x^2", "y = x^3"), col = c("blue", "red"), lty = 1)
Conclusion
In this chapter, you learned how to create and customize line graphs in R using the base R plot()
function. Line graphs are useful for displaying trends over time or continuous data and can be customized to improve their readability and aesthetics. By mastering line graphs, you can effectively communicate trends and changes in your data analysis.