1

I am new to R, so I ask you keep it simple if you can! I have the following XTS called "xts_ARM.AUTO:

                  ARM_yearly_rate  AUTO_yearly_rate        
2016-01-01        2.876923         4.313732
2017-01-01        3.198846         4.600851
2018-01-01        3.822885         5.039592
2019-01-01        3.572885         5.382536
2020-01-01        3.074717         5.105063

I executed the following:

ARM.AUTO_rates<-plot(xts_ARM.AUTO,
                      ylab="Rates",main="ARM Rates vs AUTO Rates")

Now I have a plot called ARM.AUTO_rates with two lines! How cool! However, I want to tell which line is which. Specifically, I want to display which line is ARM_yearly_rate ("the ARM rate") and which line is the ARM_yearly_rate ("the AUTO rate"). I want to color them differently and create a label, maybe even a legend. I cannot figure out how. I know this must be basic, but I could use the help!

Any help appreciated!

AaronSzcz
  • 145
  • 1
  • 8
  • Could you please provide a `dput(xts_ARM.AUTO)`? Thanks. BTW, this could help you: https://stackoverflow.com/a/15132396/13249862 – bttomio Feb 21 '21 at 22:31
  • dput(xts_ARM.AUTO) structure(c(2.87692307692308, 3.19884615384615, 3.82288461538462, 3.57288461538462, 3.07471698113208, 4.31373166447079, 4.60085134750041, 5.03959157710471, 5.38253633365901, 5.10506280193237), .Dim = c(5L, 2L), .Dimnames = list(NULL, c("ARM_yearly_rate", "AUTO_yearly_rate" )), index = structure(c(1451606400, 1483228800, 1514764800, 1546300800, 1577836800), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo")) @bttomio – AaronSzcz Feb 22 '21 at 04:54

1 Answers1

0

In order to get two lines, you need ts.plot:

xts_ARM.AUTO <-structure(c(2.87692307692308, 3.19884615384615, 3.82288461538462, 3.57288461538462, 3.07471698113208, 4.31373166447079, 4.60085134750041, 5.03959157710471, 5.38253633365901, 5.10506280193237), .Dim = c(5L, 2L), .Dimnames = list(NULL, c("ARM_yearly_rate", "AUTO_yearly_rate" )), index = structure(c(1451606400, 1483228800, 1514764800, 1546300800, 1577836800), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"))

ARM.AUTO_rates<-ts.plot(xts_ARM.AUTO,
                     ylab="Rates",main="ARM Rates vs AUTO Rates")

enter image description here

As answered here, you can use libraries xts and xtsExtra:

library(xts)
library(xtsExtra)

NEW.ARM.AUTO_rates<-plot.xts(xts_ARM.AUTO, screens = factor(1, 1), auto.legend = TRUE)

enter image description here

bttomio
  • 2,206
  • 1
  • 6
  • 17
  • 1
    This is so excellent, thank you for your help! Is there anyway I can put word labels off to the side? So above the lines can be labels "ARM rate" and "AUTO rate"? – AaronSzcz Feb 22 '21 at 22:56
  • You're welcome! You can do lots of stuff with `xts` (check [here](http://joshuaulrich.github.io/xts/plotting_basics.html)). – bttomio Feb 23 '21 at 07:43