Search code examples
rplotggplot2statisticsggfortify

choose colour when plotting multivariate time series with ggfortify as overlay


library(ggfortify)

With ggfortify, if I plot one time series, I can set the line colour as follows:

autoplot(myts1,ts.colour='blue')

I can plot two ts objects in one graph:

autoplot(cbind(myts1,myts2),facets=FALSE)

But how can I set for example the line colour for the first ts 'blue' and for the second 'red'? In the second example, ts.colour doesn't work at all.

edit: here is a working example

myts1 = filter(rnorm(100), filter=rep(1,20),circular=TRUE)
myts2 = sin(seq(0,20,length.out=100))*5+5
autoplot(cbind(myts1,myts2),facets=FALSE)

Solution

  • You can use scale_colour_manual

    When facet is disabled, autoplot colorize each series with "variable". Thus simply add scale_colour_manual.

    pallete = c('red', 'blue', 'green', 'orange')
    autoplot(Canada, facets = FALSE, size = 3) + scale_colour_manual(values=pallete)
    

    enter image description here

    Otherwise, you must specify colour = "variable" explicitly to colorize each series.

    autoplot(Canada, size = 3, ts.colour = 'variable') + scale_colour_manual(values=pallete)
    

    enter image description here