• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing How to Make a Line Graph in r using ggplot2?

To make a line graph in R you can use the ggplot() function from the ggplot2 package. This package provides a powerful and flexible framework for constructing various types of plots written by Hadley Wickham and is also termed as Grammar of Graphics is a free, open-source, and easy-to-use visualization package widely used in R Programming Language.

In this tutorial, I will explain the process of creating basic line graphs using ggplot2 and demonstrate how to customize them by adding points, changing line types, colors, sizes, and more with well-defined examples.

Key points-

  • Use the ggplot() function to initialize the plot. This function takes the dataset and aesthetics (aes) as its primary arguments.
  • Specify the x-axis and y-axis variables within the aes() function to map the data to the plot.
  • Use geom_line() to add lines to the plot. This function connects the data points with lines based on the specified aesthetics. This is the fundamental geom for creating line graphs.
  • Customize the plot by adding layers using the + operator. Additional layers can include points (geom_point()), smoothed lines (geom_smooth()), and more. Layers enable you to enhance the plot with different elements.
  • When dealing with multiple groups, use aesthetics such as color or group to distinguish between them. For example, color-coding different lines based on a grouping variable can enhance the clarity of the plot.
  • Apply themes to control the overall appearance of the plot. For instance, theme_minimal() provides a clean and minimalistic appearance.
  • Add labels to the plot using labs() to provide a title, x-axis label, and y-axis label for better interpretation.

Create a Basic Line Graph using R ggplot

Let’s create a simple dataset with time points (Time) and corresponding random cumulative values (Value) and use the data.frame function to create a data frame from given data.


# Create a data frame
set.seed(123)
df1 <- data.frame(
  Time = 1:10,
  Value = cumsum(rnorm(10))
)
df1

Yields below output.

line graph in r ggplot

The ggplot() function is used to initiate the plot, 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)
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

Alternatively, 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 various 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.

make a line graph in r

Adding Points to the Line Graph using ggplot

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.

make a line graph in r

Adding Smooth Line (Loess)

You can also provide a smooth line(Loess) to the 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.

make a line graph in r

Customizing Axis Limits of Line Graph 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 the creation and customization of line graphs using ggplot2 in R empowers you to communicate your data insights effectively. Whether adjusting line types, and colors or incorporating additional layers, the ggplot2 package provides a versatile and intuitive platform for crafting compelling visualizations. Experimenting with different customization options allows you to tailor your line graphs to effectively convey the story behind your data.

Happy learning!!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.