• Post author:
  • Post category:R Programming
  • Post last modified:May 25, 2024
  • Reading time:16 mins read
You are currently viewing Line Graphs in R

In R, line graphs are essential tools for visualizing trends and patterns in data, particularly when exploring continuous variables like time. The plot() function from the base R and the ggplot() function from the ggplot2 package are commonly used to create line graphs. In this article, I will explain to you the basics of creating and customizing line graphs using both approaches.

Advertisements

Key points-

  • The plot() function is a base R function for creating plots. x and y are vectors or matrices representing the data points.
  • You can customize the axes using arguments like xlab and ylab for axis labels, main for the main title, and xlim or ylim for setting axis limits.
  • Points can be added to the line using points() function after the initial plot().
  • Use ggplot() to initialize the plot and add layers. Specify aesthetics (e.g., aes(x = variable1, y = variable2)) inside ggplot() to map variables to visual properties.
  • Use geometric objects (geoms) to represent data points and lines. For line graphs, use geom_line().

plot() Function in R

The plot() function in R is a versatile function used to create various types of plots, including scatter plots, line plots, bar plots, histograms, and more. It is a fundamental plotting function in R and is often the starting point for creating visualizations.

Syntax of plot() function


# Syntax of plot()
plot(v, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")

Parameters

  • vThis parameter contains only the numeric values
  • type: Specifies the type of plot (e.g., "p" for points, "l" for lines, "b" for both, etc.).
  • main: Main title of the plot.
  • xlab and ylab: Labels for the x and y axes.
  • xlim and ylim: Limits for the x and y axes.
  • col: Color of points or lines.
  • pch: Symbol or character to be used for points.

Return Value

The plot() function returns the plotted object.

Let’s create a simple line graph using sample data x and y. The plot() function is used with the type = "l" parameter to generate a basic line graph.


# Create sample data
x <- 1:10
y <- c(2, 4, 7, 3, 6, 8, 5, 9, 10, 3)

# Create a basic line graph
plot(x, y, type = "l")

Yields below output.

line graph in r

Customize the Line Graph in R

To customize a line graph in R, you can modify various parameters such as col, lty, lwd, main, X-axis, and Y-axis of the plot() function. These customizations can improve the visual appeal and information presented by the graph.


# Customize the line graph
x <- 1:10
y <- c(2, 4, 7, 3, 6, 8, 5, 9, 10, 3)

# Create a basic line graph
plot(x, y, type = "l", col = "blue", lty = 1, lwd = 2, xlab = "X-axis", ylab = "Y-axis", main = "Basic Line Graph")

The above code x is a sequence from 1 to 10 and y contains some arbitrary values. The plot() function is used with type = "l" to create a line graph. col, lty, and lwd are used to set color, line type, and line width, respectively. xlab, ylab, and main set the axis labels and the main title.

Yields below output.

line graph in r

Create a Line Graph with Multiple Lines

To create multiple lines on a single graph, the plot() function is initially used with one set of data, and the lines() function is employed to add a second set of data. This process will make the capability of the plot() function to handle multiple lines within the same plot.


# Sample data
x <- 1:5
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)

# Create a line graph with multiple lines
plot(x, y1, type = "o", col = "blue", lty = 1, lwd = 2, xlab = "X-axis", ylab = "Y-axis", main = "Multiple Lines Graph")

# Add a second line to the existing graph
lines(x, y2, type = "o", col = "red", lty = 2, lwd = 2)

The above code x is a sequence from 1 to 5, and there are two sets of y values (y1 and y2). The code is used plot() to create the initial line graph with the first set of y values (y1). Then, lines() is used to add a second line (y2) to the existing graph. The first line is blue, and the second line is red.

Yields below output.

line graph in r

Add Legends to Line Graph

The legend() function is used to add a legend to the graph, specifying the names, colors, line types, and line widths of the two lines. Legends are crucial for interpreting graphs with multiple lines. This enhances the clarity of the plot and aids in understanding the represented data.


# Add legends to line graph
# Sample data
x <- 1:5
y1 <- c(2, 4, 6, 8, 10)
y2 <- c(1, 3, 5, 7, 9)

# Create a line graph with multiple lines
plot(x, y1, type = "o", col = "blue", lty = 1, lwd = 2, xlab = "X-axis", ylab = "Y-axis", main = "Multiple Lines Graph")

# Add a second line to the existing graph
lines(x, y2, type = "o", col = "red", lty = 2, lwd = 2)
legend("topleft", legend = c("Line 1", "Line 2"), col = c("blue", "red"), lty = c(1, 2), lwd = 2)

Yields below output.

line graph in r

Create a Line Graph using ggplot2

Alternatively, you can use the ggplot() function from ggplot2 package to create the line graph , specifying aesthetics for the x and y axes with aes(), adding a line with geom_line(), and providing a title with ggtitle(). Before using the ggplot() function you need to install and load the ggplot2 package as install.packages("ggplot2") and library(ggplot2) respectively.


# Create a line graph
# Install and load the ggplot2 package 
# install.packages("ggplot2")
library(ggplot2)

# Create a data frame
set.seed(123)
df1 <- data.frame(
  Time = 1:10,
  Value = cumsum(rnorm(10))
)
df1
ggplot(df1, aes(x = Time, y = Value)) +
  geom_line() +
  ggtitle("Basic Line Graph")
  • ggplot() Function: Initializes the plot with the specified dataset (df1) and aesthetic mappings.
  • aes(x = Time, y = Value): Specifies the x-axis as Time and y-axis as Value.
  • geom_line(): Adds a line to the plot based on the specified aesthetics.
  • ggtitle("Basic Line Graph"): Adds a title to the plot.

Yields below output.

line graph in r ggplot

Line Graph with Color and Customization using R ggplot

You can customize the line graph by changing line types, colors, and sizes using the ggplot2 package. The geom_line() function accepts the linetype, color, and size arguments to specify the line style, color, and size respectively.

Line Type

In R, ggplot2 provides multiple line types for customizing the type of line graph. For example dotted, two dash, dashed, etc. Let’s pass this attribute with a specified value.


# Customize the line type
library(ggplot2)
ggplot(df1, aes(x=Time, y=Value, group=1)) +
  geom_line(linetype = "dotted")+
  ggtitle("Basic Line Graph")

Yields below output.

line graph in r ggplot

Line Color

You can pass the argument color with the desired color specified in double quotes (” “) into the geom_line( ). It will visualize the line graph with the desired color.


# Customize the the line color
ggplot(df1, aes(x=Time, y=Value, group=1)) +
  geom_line(color = "orange")+
  ggtitle("Basic Line Graph")

Yields below output.

make a line graph in r

Line Size

You can provide a specified value to the size parameter inside geom_line( ) to change the size of the line graph.


# Customize the line size
ggplot(df1, aes(x=Time, y=Value, group=1)) + 
  geom_line(color = "orange", size = 2)+ 
  ggtitle("Basic Line Graph")

Yields below output.

line graph in r

Customize the Line Graph by Attaching New Points

Similarly, you can add the points to the line in a graph using geom_point(). This additional information layer can improve the plot’s interpretability and highlight specific data points.


# Adding points to the line plot
library(ggplot2)
ggplot(df1, aes(x = Time, y = Value)) +
  geom_line() +
  geom_point() +
  ggtitle("Basic Line Graph") 

Yields below output.

line graph in r

Adding Smooth Line (Loess)

You can also provide a smooth line(Loess) to the line plot using geom_smooth(). This can help identify trends in the data and provide a clearer picture of the overall pattern.


# Adding a smooth line (Loess) to the plot
ggplot(df1, aes(x = Time, y = Value)) +
  geom_line() +
  geom_smooth() +
  ggtitle("Basic Line Graph") 

Yields below output.

 line graph in r

Customizing Axis Limits using ggplot

You can customize axis limits to focus on specific ranges of data. This can be achieved by setting the ylim() with desired y-axis limits, ensuring the plotted data is presented within the specified range.


# Customizing axis limits
ggplot(df1, aes(x = Time, y = Value)) +
  geom_line() +
  ylim(c(-5, 15)) +
  ggtitle("Basic Line Graph") 

Yields below output.

line graph using ggplot

Multiple Line Graph using ggplot

You can use the ggplot2 package to create multiple line plots easily. Here’s an example using a simple dataset that has three columns first one for the x-axis, the second one for the y-axis, and the final one for grouping purposes. Let’s use this data to create a multiple-line graph.


# Create a line graph with multiple lines for each group
library(ggplot2)

# Sample dataset
data <- data.frame(
  Time = rep(seq(1, 10), each = 3),
  Value = rnorm(30),
  Group = rep(c("A", "B", "C"), each = 10)
)
data

# Create a line graph with multiple lines for each group
ggplot(data, aes(x = Time, y = Value, color = Group)) +
  geom_line() +
  labs(title = "Multiple Line Graphs",
       x = "Time",
       y = "Value") +
  theme_minimal()

Yields below output.

line graph using ggplot

Conclusion

In this article, I have explained Creating effective line graphs in R involves understanding the capabilities of both the base plot() function and the more flexible ggplot2 package. Customization options, handling multiple lines, adding legends, and exploring variations contribute to producing insightful visualizations for data analysis and interpretation. By mastering these techniques, you can effectively communicate trends and patterns in your datasets.

Happy Learning!!