How to Create a GGPlot with Multiple Lines



How to Create a GGPlot with Multiple Lines

This tutorial describes how to create a ggplot with multiple lines.

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Load ggplot2 package

Load ggplot2 and set the default theme:

library(ggplot2)
theme_set(theme_minimal())

Data

The US economics time series datasets are used. This is a data frame with 478 rows and 6 variables.

head(economics)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <int>   <dbl>   <dbl>    <int>
## 1 1967-07-01  507. 198712    12.5     4.5     2944
## 2 1967-08-01  510. 198911    12.5     4.7     2945
## 3 1967-09-01  516. 199113    11.7     4.6     2958
## 4 1967-10-01  513. 199311    12.5     4.9     3143
## 5 1967-11-01  518. 199498    12.5     4.7     3066
## 6 1967-12-01  526. 199657    12.1     4.8     3018
  • date: Month of data collection
  • psavert: personal savings rate
  • pce: personal consumption expenditures, in billions of dollars
  • unemploy: number of unemployed in thousands
  • uempmed: median duration of unemployment, in weeks
  • pop: total population, in thousands

Basic line plot

Line plot of the variable ‘psavert’ by date:

ggplot(data = economics, aes(x = date, y = psavert))+
  geom_line()

Plot with multiple lines

Well plot both ‘psavert’ and ‘uempmed’ on the same line chart.

  • Solution 1: Make two calls to geom_line():
ggplot(economics, aes(x=date)) + 
  geom_line(aes(y = psavert), color = "darkred") + 
  geom_line(aes(y = uempmed), color="steelblue", linetype="twodash") 

  • Solution 2:
    1. Prepare the data using the tidyverse packages. Collapses the two variables ‘psavert’ and ‘uempmed’ into key-value pairs
    2. Visualize using one geom_line() call
# Data preparation
library("tidyverse")
df <- economics %>%
  select(date, psavert, uempmed) %>%
  gather(key = "variable", value = "value", -date)
head(df)
## # A tibble: 6 x 3
##   date       variable value
##   <date>     <chr>    <dbl>
## 1 1967-07-01 psavert   12.5
## 2 1967-08-01 psavert   12.5
## 3 1967-09-01 psavert   11.7
## 4 1967-10-01 psavert   12.5
## 5 1967-11-01 psavert   12.5
## 6 1967-12-01 psavert   12.1
# Visualization
ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variable, linetype = variable)) + 
  scale_color_manual(values = c("darkred", "steelblue"))



Version: Français





Comments ( 16 )

  • Scot

    How can you change the size(thickness) of one of the lines in the tidyverse version?

    • Kassambara

      You can specify, for example, the argument size = 3 in the function geom_line()

  • Abdi Dahir

    I have used the first method but i have difficulties with the legend. As you can see, there is no legend indicating the column each line represents. How can i add legend for the first case (method)?

    • Eric Reynaud

      First, you need to set the colors of each line inside aes(…). That will create the legend, but the colors wont be the expected ones . To get them, add
      + scale_color_identity(guide = legend())
      at the end of ggplot() block code

      • QB

        This doesnt work. Argument legend is missing.

  • Rasin

    Thank you Sir
    it is very simple and explained very well

  • Keiran

    If i want to add more than 2 lines what should i do, i am trying with the second method and it works fine with only 2 but won’t show others

    • Kassambara

      Hi, please make sure you have specified as many colors as the number of lines. This works for me:

      library("tidyverse")
      df <- economics %>%
        select(date, psavert, uempmed) %>%
        mutate(uempmed_2 = 2*uempmed) %>%
        gather(key = "variable", value = "value", -date)
      
      ggplot(df, aes(x = date, y = value)) + 
        geom_line(aes(color = variable, linetype = variable)) + 
        scale_color_manual(values = c("darkred", "steelblue", "green"))

  • Rmann

    Thank you for this it is very well explained.
    I have following question. In the solution 1 there is no legend for the lines whereas the solution 2 has a legend.
    I was wondering if you can show how to put a legend and chart title etc in Solution 1.
    Thanks

    • Kassambara

      Thank you for the positive comment, highly appreciated! Here’s how I’ll add a legend: I specify the variable color in aes() and give it the name I want to be displayed in the legend.

      library(ggplot2)
      theme_set(theme_minimal())
      data(economics)
      ggplot(economics, aes(x = date)) +
        geom_line(aes(y = psavert, color = "psavert")) +
        geom_line(aes(y = uempmed, color = "uempmed"), linetype = "twodash") +
        scale_color_manual(values = c("darkred", "steelblue"))

      • Rmann

        Thank you very much for the quick response. I am a beginner and trying to explore R. I am glad I learned this otherwise it would have been very demotivating.
        Thanks a ton

  • Rick

    Is there a way to display the last value of each line in the plot? I am new to R and have not found any workable solution. Thank you in advance!

  • Shannon Pederson

    This was terrific! I believe I got your both your examples of coding to work because your variables were all continuous. What if your X variable is categorical (e.g. gender)?

  • Sam

    How do you change the name of the legend values? (I.e. psavert, uempmed, etc.)

  • Shehzeen

    if I want to change word color in legend then what can we do as u scale_color_manual is there any option to change the heading of legends

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments