How to plot multiple lines form time series data in ggplot

I have below data series.

I want them to plot with date on X Axis, value on Y axis and difference line for each year.

ggplot(Data, mapping=aes(x=Date, y=Value))+geom_line(aes(color=Year))

This code is giving single line only

Pls advise

Date Value Year
28-May-21 112 2020
27-May-21 112 2020
26-May-21 115 2020
28-May-21 115 2019
27-May-21 115 2019
26-May-21 115 2019
28-May-21 110 2018
27-May-21 100 2018
26-May-21 110 2018
28-May-21 115 2017
27-May-21 115 2017
26-May-21 115 2017
28-May-21 110 2016
27-May-21 115 2016
26-May-21 100 2016

Try this version:

library(ggplot2)

ggplot(Data, mapping=aes(x=Date, y=Value, group = Year))+geom_line(aes(color=factor(Year)))
library(tidyverse)
library(lubridate)

# Sample data in a copy/paste friendly format, replace this with your own data frame
Data <- data.frame(
  stringsAsFactors = FALSE,
              Date = c("28-May-21","27-May-21",
                       "26-May-21","28-May-21","27-May-21","26-May-21",
                       "28-May-21","27-May-21","26-May-21","28-May-21","27-May-21",
                       "26-May-21","28-May-21","27-May-21","26-May-21"),
             Value = c(112,112,115,115,115,115,
                       110,100,110,115,115,115,110,115,100),
              Year = c(2020,2020,2020,2019,2019,
                       2019,2018,2018,2018,2017,2017,2017,2016,2016,2016)
)

#Relevant code
Data %>% 
    mutate(Date = dmy(Date), 
           Year = factor(Year)) %>% 
    ggplot(mapping = aes(x = Date, y = Value)) + 
    geom_line(aes(color = Year))

Created on 2021-05-28 by the reprex package (v2.0.0)

Sorry, data pasted was wrong.

Data
Date Value Year
1 28-May-21 112 2020
2 27-May-21 112 2020
3 26-May-21 115 2020
4 28-May-20 115 2019
5 27-May-20 115 2019
6 26-May-20 115 2019
7 28-May-19 110 2018
8 27-May-19 100 2018
9 26-May-19 110 2018
10 28-May-18 115 2017
11 27-May-18 115 2017
12 26-May-18 115 2017
13 28-May-17 110 2016
14 27-May-17 115 2016
15 26-May-17 100 2016

Above is data. And now if i use solution given in two commetns below, i do get Dates as continuous charts, while i need that basus Year, dates are used in X Axis. and dates can be differnet in real data for differnet years.

please see my latest reply. Data posted earlier was not right and thus it do not help to solve the problem.

I don't understand what your problem is, if you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Data <- data.frame( stringsAsFactors = FALSE,Date = c("28-May-21","27-May-21","26-May-21","27-May-20","26-May-20","25-May-20","27-May-19","26-May-19","24-May-19","26-May-18","25-May-18","24-May-18","28-May-17","27-May-17","26-May-17"),Value = c(112,108,115,95,103,112,85,95,110,105,108,112,85,95,100),Year = c(2020,2020,2020,2019,2019,2019,2018,2018,2018,2017,2017,2017,2016,2016,2016))

Data %>% mutate(Date = dmy(Date), Year = factor(Year)) %>% ggplot(mapping = aes(x = Date, y = Value)) + geom_line(aes(color = Year))

image

But what i need is Dates on x-Axis and values on y Axis and Different Lines for each year.

Thanks for your reply.

Is this what you mean?

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
Data <- data.frame( stringsAsFactors = FALSE,
                    Date = c("28-May-21","27-May-21","26-May-21","27-May-20","26-May-20",
                             "25-May-20","27-May-19","26-May-19","24-May-19","26-May-18",
                             "25-May-18","24-May-18","28-May-17","27-May-17","26-May-17"),
                    Value = c(112,108,115,95,103,112,85,95,110,105,108,112,85,95,100),
                    Year = c(2020,2020,2020,2019,2019,2019,2018,2018,2018,2017,2017,
                             2017,2016,2016,2016))

Data %>% mutate(Date = dmy(Date), Year = factor(Year),MayDay=day(Date)) %>% 
  ggplot(mapping = aes(x = MayDay, y = Value,group=Year)) + geom_line(aes(color = Year))

Created on 2021-05-29 by the reprex package (v0.3.0)

Thanks . This solved the problem to some extent. And i am still not clear why ggplot is not taking dates based on grouping of Year.

But i will not get the desired output in case data is as below for different months.

So, on X Axis i do not need only the day, i need full date which is what i was trying to put in my code.

For example, if data is as below, your code wont help.

data.frame( stringsAsFactors = FALSE,
Date = c("28-May-21","27-May-21","26-May-21","15-Jun-20","14-Jun-20",
"13-Jun-20","13-Jul-19","14-Jul-19","15-Jul-19","26-May-18",
"25-May-18","24-May-18","28-Jun-17","27-Jun-17","26-Jun-17"),
Value = c(112,108,115,95,103,112,85,95,110,105,108,112,85,95,100),
Year = c(2020,2020,2020,2019,2019,2019,2018,2018,2018,2017,2017,
2017,2016,2016,2016))

To be frank, I'm not totally sure I understand what you want. But here's an attempt with your latest dataset:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

sample_data <- data.frame(Date = c("28-May-21", "27-May-21", "26-May-21", "15-Jun-20", "14-Jun-20", "13-Jun-20", "13-Jul-19", "14-Jul-19", "15-Jul-19", "26-May-18", "25-May-18", "24-May-18", "28-Jun-17", "27-Jun-17", "26-Jun-17"),
                          Value = c(112, 108, 115, 95, 103, 112, 85, 95, 110, 105, 108, 112, 85, 95, 100),
                          Year = c(2020, 2020, 2020, 2019, 2019, 2019, 2018, 2018, 2018, 2017, 2017, 2017, 2016, 2016, 2016))

sample_data %>%
    mutate(Date = as.Date(x = Date,
                          format = "%d-%b-%y"),
           Date_Factor = factor(x = format(x = Date,
                                           format = "%b %d"),
                                levels = paste(rep(x = month.abb,
                                                   each = 31),
                                               formatC(x = 1:31,
                                                       width = 2,
                                                       flag = "0"))),
           Year = as.factor(x = Year)) %>%
    ggplot(mapping = aes(x = Date_Factor,
                         y = Value)) +
    geom_line(mapping = aes(group = Year,
                            colour = Year))

Created on 2021-05-30 by the reprex package (v2.0.0)

NB: You may note that I used 31*12 days in a year. That's just because it'd do the job quickly. You can correct it, of course.

1 Like

Thanks it is working now. But i did not understand the concept of below code, if you cud help to explain.

mutate(Date = as.Date(x = Date,
format = "%d-%b-%y"),
Date_Factor = factor(x = format(x = Date,
format = "%b %d"),
levels = paste(rep(x = month.abb,
each = 31),
formatC(x = 1:31,
width = 2,
flag = "0"))),

Rgds

I got it. We need to create this as factor otherwise we wont get the date from first to last in graph.

Thanks for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.