# 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 to 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``` 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 for each group 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``` 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.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

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 with multiple groupings for the geom_line function. 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 the Statistics Globe Newsletter

• Do I see it correctly?
January 17, 2021 10:35 pm

I copied the procedure in your Example 1, and noticed my plots had color problems. I returned to your site, and see that your results have the same problems I did. Your seem to call for y1 to be red; however, the y1 line in the chart appears to be blue-green (not really even “blue”). Likewise, the other line appears switched and the legend appears to be mixed up as well. I am not sure yet what causes the problems.

• Wow, thanks a lot for this! I didn’t notice this error in my code when I was creating the tutorial.

Thanks again!

Joachim

• Do I see it correctly?
January 17, 2021 10:46 pm

I think I found the correct program:

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

• Thanks a lot, I have just corrected my code (see comment above).

• S
March 18, 2021 9:58 am

How do you deal with NA in your time series?

• Sonwil Yakubu
August 19, 2021 12:02 pm

I want to determine structural breaks in mean and variance at the end of sample period that is outside the admissible region of (0.15T, 0.85T).

I want to determine the size of the breaks as well and estimate breaks size on forecasting as well. Any help?