【问题标题】:How to label and change colors of lines in xts plot如何在 xts 图中标记和更改线条的颜色
【发布时间】:2021-05-24 02:31:28
【问题描述】:

我是 R 的新手,所以如果可以的话,请尽量保持简单!我有以下称为“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

我执行了以下操作:

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

现在我有一个名为 ARM.AUTO_rates 的两行图!挺酷的!但是,我想告诉哪一行是哪一行。具体来说,我想显示哪一行是 ARM_yearly_rate (“ARM 汇率”),哪一行是 ARM_yearly_rate (“自动汇率”)。我想给它们涂上不同的颜色并创建一个标签,甚至是一个传奇。我不知道怎么做。我知道这一定是基本的,但我可以使用帮助!

任何帮助表示赞赏!

【问题讨论】:

  • 您能提供一个dput(xts_ARM.AUTO)吗?谢谢。顺便说一句,这可以帮助你:stackoverflow.com/a/15132396/13249862
  • dput(xts_ARM.AUTO)结构(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= "25"), 123 c("xts", "zoo")) @bttomio

标签: r plot plotly tidyverse


【解决方案1】:

为了得到两条线,你需要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")

正如here 的回答,您可以使用库xtsxtsExtra

library(xts)
library(xtsExtra)

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

【讨论】:

  • 这太棒了,谢谢你的帮助!反正我可以把文字标签放在一边吗?那么上面的行可以是标签“ARM rate”和“AUTO rate”?
  • 不客气!你可以用xts 做很多事情(检查here)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多