2
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)
bfgler
  • 53
  • 8
  • 2
    can you include in your post a subset of your dataset, enough to reproduce the error? – MLavoie Jan 05 '16 at 16:21
  • and instead of cbind (your datasets) maybe your should merge them by the date. In the ggfortify manual, there is a example with 4 timeseries. – MLavoie Jan 05 '16 at 16:23
  • To elaborate on [Mlavoie](https://stackoverflow.com/users/3396821/mlavoie)’s point about _ reproducing the error_. If you supply a minimal reproducible example to go along with your question we can use that to show you how it might be possible to answer your question––it's more likely that we will be able to help you. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Jan 05 '16 at 16:29
  • Thanks for the advices. I added a working example, but also already got the solution for my problem by the user sinhrks. – bfgler Jan 06 '16 at 16:57

1 Answers1

2

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

sinhrks
  • 1,306
  • 10
  • 5