Draw Multiple Variables as Lines to Same ggplot2 Plot in R (2 Examples)

 

In this tutorial you’ll learn how to plot two or more lines to only one ggplot2 graph in R programming.

The article is structured as follows:

You’re here for the answer, so let’s get straight to the exemplifying R syntax.

 

Example Data, Packages & Default Plot

The following data is used as basement for this R programming tutorial:

set.seed(6532465)                    # Create example data
data <- data.frame(x = 1:50,
                   y1 = sort(rnorm(50)),
                   y2 = sort(rnorm(50, 0.5)))
head(data)                           # Head of example data
#   x        y1         y2
# 1 1 -2.233737 -0.9549823
# 2 2 -1.836179 -0.9039053
# 3 3 -1.828040 -0.7433467
# 4 4 -1.691616 -0.6736192
# 5 5 -1.522380 -0.6325588
# 6 6 -1.437409 -0.6307781

Have a look at the previous output of the RStudio console. It shows that our example data consists of three columns. The variable x ranges from 1 to 50 and represents the x-axis values of our plot. The variables y1 and y2 represent the y-axis values of two different lines we will draw in this tutorial.

If we want to create a plot of our data with the ggplot2 package, we also have to install and load ggplot2:

install.packages("ggplot2")          # Install ggplot2 package
library("ggplot2")                   # Load ggplot2 package

Now, we can move on to the plotting of our data.

 

Example 1: Plotting Two Lines in Same ggplot2 Graph Using geom_line() Multiple Times

In this Example, I’ll illustrate how draw two lines to a single ggplot2 plot using the geom_line function of the ggplot2 package. For this, we have to specify our x-axis values within the aes of the ggplot function. The values for the y-axis are specified within the two geom_line commands:

ggp1 <- ggplot(data, aes(x)) +       # Create ggplot2 plot
  geom_line(aes(y = y1, color = "red")) +
  geom_line(aes(y = y2, color = "blue"))
ggp1                                 # Draw ggplot2 plot

 

r graph figure 1 draw multiple lines same ggplot2 r

 

The output of the previous R programming syntax is shown in Figure 1: It’s a ggplot2 line graph showing multiple lines.

 

Example 2: Plotting Two Lines in Same ggplot2 Graph Using Data in Long Format

In Example 1 you have learned how to use the geom_line function several times for the same graphic. However, this methodology is not convenient for a large number of lines, since we would have to specify the geom_line function for each line we want to draw.

The following syntax shows a more general approach for the plotting of multiple lines in a ggplot2 plot by reshaping our data frame from wide to long format.

We will use the functions of the reshape2 package to transform our data from wide to long format. Hence, we first have to install and load the reshape2 package:

install.packages("reshape2")         # Install & load reshape2 package
library("reshape2")

Now, we can convert our data from wide to long format as shown below:

data_long <- melt(data, id = "x")    # Convert data to long format
head(data_long)                      # Head of long data
#   x variable     value
# 1 1       y1 -2.233737
# 2 2       y1 -1.836179
# 3 3       y1 -1.828040
# 4 4       y1 -1.691616
# 5 5       y1 -1.522380
# 6 6       y1 -1.437409

Finally, we can use our long data to draw a ggplot2 graph containing multiple lines as shown below:

ggp2 <- ggplot(data_long,            # Create ggplot2 plot
               aes(x = x,
                   y = value,
                   color = variable)) +
  geom_line()
ggp2                                 # Draw ggplot2 plot

 

r graph figure 2 draw multiple lines same ggplot2 r

 

As shown in Figure 2, the previous R programming syntax created a similar ggplot2 plot as in Example 1. However, this time the R code is more general and can easily be applied to large data sets.

 

Video & Further Resources

In case you need further info on the R programming code of this article, you may have a look at the following video of my YouTube channel. In the video, I show the topics of this page.

 

The YouTube video will be added soon.

 

Besides the video, you may want to read the related articles on this website. A selection of tutorials on related topics such as dates, graphics in r, regression models, and lines can be found below.

 

In this R tutorial you learned how to create a ggplot2 plot containing multiple lines. Don’t hesitate to let me know in the comments, in case you have further questions or comments. Besides that, please subscribe to my email newsletter for updates on new tutorials.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top