youtube video link with explanations for these examples Video link https://youtu.be/4kEdm9NJE28

Multiple line charts in R

Packages used

library(ggplot2)
library(dplyr)
library(tidyr)

Create the data set

# Create the sample data

df <- data.frame(date     =  seq(as.Date('2021-01-01')
                                 , as.Date('2021-01-31')
                                 , by = "day")
                 , patients = as.integer(runif(31
                                               ,min = 1000
                                               , max = 1200
                 ))
                 , deaths   = as.integer(runif(31
                                               ,min = 100
                                               , max = 120
                 ))
)

df
##          date patients deaths
## 1  2021-01-01     1097    114
## 2  2021-01-02     1089    108
## 3  2021-01-03     1041    110
## 4  2021-01-04     1075    116
## 5  2021-01-05     1093    118
## 6  2021-01-06     1097    114
## 7  2021-01-07     1128    118
## 8  2021-01-08     1012    103
## 9  2021-01-09     1158    118
## 10 2021-01-10     1045    111
## 11 2021-01-11     1159    117
## 12 2021-01-12     1139    103
## 13 2021-01-13     1046    118
## 14 2021-01-14     1047    101
## 15 2021-01-15     1054    118
## 16 2021-01-16     1092    116
## 17 2021-01-17     1041    107
## 18 2021-01-18     1199    113
## 19 2021-01-19     1020    102
## 20 2021-01-20     1045    100
## 21 2021-01-21     1081    110
## 22 2021-01-22     1005    103
## 23 2021-01-23     1192    112
## 24 2021-01-24     1064    104
## 25 2021-01-25     1001    116
## 26 2021-01-26     1081    100
## 27 2021-01-27     1091    119
## 28 2021-01-28     1163    109
## 29 2021-01-29     1116    119
## 30 2021-01-30     1049    111
## 31 2021-01-31     1188    106

Method 1 for creating multiple lines

pl <- ggplot(data = df, aes(x = date))
pl <- pl + geom_line(aes(y = patients))
pl <- pl + geom_point(aes(y = patients))
pl <- pl + geom_line(aes(y = deaths))
pl <- pl + geom_point(aes(y = deaths))
pl <- pl + theme_classic()
pl

pl <- ggplot(data = df, aes(x = date))
pl <- pl + geom_line(aes(y = patients, colour = "one"))
pl <- pl + geom_point(aes(y = patients, colour = "one"))
pl <- pl + geom_line(aes(y = deaths, colour = "two"))
pl <- pl + geom_point(aes(y = deaths, colour = "two"))
pl <- pl + theme_classic()
pl <- pl + scale_color_manual(name ="variable"
                              , labels= c("Death", "Patients")
                              , values = c("green", "black"))

pl

Transform the data to long

df2  <- df%>%
  tidyr::pivot_longer(  cols = c(patients, deaths))

Method 2 for creating multiple lines

pl <- ggplot(data = df2 , aes(x = date, y = value, color = name))
pl <- pl + geom_line()
pl <- pl + geom_point()
pl <- pl + theme_classic()
pl <- pl + scale_color_manual(values = c("blue", "red"))
pl

# same chart with some more formatting
pl <- ggplot(data = df2 , aes(x = date, y = value, color = name))
pl <- pl + geom_line()
pl <- pl + geom_point()
pl <- pl + theme_classic()
pl <- pl + labs(x = "Date", y = "Count")
pl <- pl + labs(title      = "Daily patient count and deaths"
                , subtitle = paste0("n=" , sum(df$patients))
                , caption  = '@techanswers88')
pl <- pl + expand_limits(y = 0)
pl <- pl + scale_color_manual(values = rev(c("blue", "red")))
pl <- pl + labs(color = "Variables")
pl <- pl + scale_y_continuous(labels = scales::comma)
pl