Home > Software engineering >  Use different colour in part of a time series plot
Use different colour in part of a time series plot

Time:08-22

I am trying unsuccessfully to apply different colour a for distinct part of my time series plot. Below is my code

library(ggplot2)
dat = data.frame('date' = as.Date(c('2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01', '2004-01-01')),
                    'value' = c(10, 5, 12, 2, 23),
                    'Part' = c('NO', 'NO', 'YES', 'YES', 'YES'))

ggplot(data = dat)  
    geom_line(aes(x = date, y = value, color = Part))

With this I am getting below plot,

enter image description here

However I wanted to get a single line plot with different colour based on the column Part in data.

Is there any way to perform this?

CodePudding user response:

Another option is saying group = 1 like this:

library(ggplot2)
ggplot(data = dat)  
  geom_line(aes(x = date, y = value, color = Part, group = 1))

Created on 2022-08-21 with enter image description here

  • Related