楼主: surah
4436 4

[问答] 求问使用xts包画图如何增加y轴 [推广有奖]

  • 10关注
  • 8粉丝

已卖:424份资源

硕士生

11%

还不是VIP/贵宾

-

威望
0
论坛币
26959 个
通用积分
0.8400
学术水平
2 点
热心指数
4 点
信用等级
2 点
经验
1918 点
帖子
142
精华
0
在线时间
122 小时
注册时间
2011-11-9
最后登录
2017-5-2

楼主
surah 发表于 2013-10-31 11:08:31 |AI写论文
100论坛币
我使用的是R studio 0.97551,运行在Windows XP系统下。

表示最近尝试想把多个时间序列画在一张plot上,代码如下(方便各位操作使用的是系统自带的Canada数据包,实际上用的是自己的time series)
library(xts)  library(xtsExtra)
library(sandwich)
library(urca)
library(timeSeries)
library(timeDate)
library(zoo)
library(MASS)
library(strucchange)
library(lmtest)
library(vars)
data(Canada)  #加载一个系统自带的数据包
plot.xts(Canada,screens=1)

虽然最后可以达到我想要的目的,但是很明显一个y轴是不够的。当我想用axis语句添加y轴的时候貌似语句失灵了,我查过xts package的解释,大概是说用了这个数据包(包括quantmod)R自带的画图语句就失灵了。
求问版上的众位高手,这种问题怎么解决?我比较心水xts包的绘图功能,但是y轴不能画真的是个问题;传统方法的plot+line貌似对于时间序列处理效果也不是很理想(就画图效果而言)
谢谢!

p.s  试过了其他主流方法如par+line,或者各种R语言推荐的


另外,求问以下几个问题(金币已经追加):

1。如果我用这段代码绘图,那么Rsweave文件对应的代码还是适用于绘图的那一套代码吗?因为我曾经试过生成pdf,总有问题

2。如果低版本的R是不是无法加载这么多的package,也不能生成pdf文件?

最佳答案

zhangyangsmith 查看完整内容

I have little knowledge on time series or ananlysis of economic data. But I am interested to do (all kinds of) plots in R. I find it quite rare that one given plot cannot be implemented in R. There must be a reason that the author of the packages make it impossible to add another axis in the plot yet I am not able to give any hint. With all that said, if you do want to do the plot in your way you ...
关键词:Time Series timeseries sandwich quantmod Library 如何

沙发
zhangyangsmith 发表于 2013-10-31 11:08:32
I have little knowledge on time series or ananlysis of economic data. But I am interested to do (all kinds of) plots in R. I find it quite rare that one given plot cannot be implemented in R. There must be a reason that the author of the packages make it impossible to add another axis in the plot yet I am not able to give any hint.
With all that said, if you do want to do the plot in your way you may need to construct it piece by piece all by YOURSELF. The following code will produce something that is quite similar to what you will get from plot.xts (at least to me). There is no time series structure involved as I am not familiar with it. BTW, the "Canada" data is provided with vars package not with R base installation.
  1. # Enlarge right margin for additional axis
  2. par(mar = c(5, 4, 4, 4) + 0.1)

  3. # Set the limits of axes
  4. plot(c(0, 84), c(0, 6), type = "n", xlab = "", ylab = "", axes = F, xlim = c(0, 84), ylim = c(-0.5, 6.5))

  5. # Minor ticks for x-axis
  6. axis(1, at = c(0:84), labels = F, col = "#BBBBBB")

  7. # Major ticks for x-axis
  8. axis(1, at = c(0:21*4), labels = sapply(0:21, function(i) ifelse(i %% 4, NA, 1980 + i)),
  9.      lwd = 1)

  10. # Y-axis on the left
  11. axis(2, at = c(0:6), labels = 930 + c(0:6)*5)

  12. # Corresponding axis title - colored same as data using this axis
  13. mtext("e", side = 2, line = 3, col = "red")

  14. # Y-axis on the right
  15. axis(4, at = c(0:6), labels = c(0:6) + 7)

  16. # Corresponding axis title - colored same as data using this axis
  17. mtext("U", side = 4, line = 3, col = "blue")

  18. # Vertical lines of grid
  19. abline(v = c(0:21*4), lty = 4, col = "grey")

  20. # Horizontal lines of grid
  21. abline(h = c(0:6), lty = 4, col = "grey")

  22. # Add box to make it more R-style
  23. box()

  24. # Plot first series - manually manage the transform of the data
  25. lines(c(0:83), (Canada[, "e"] - 930)/5, col = "red")

  26. # Plot second series - manually manage the transform of the data
  27. lines(c(0:83), Canada[, "U"] - 7, col = "blue")
复制代码
Here I have mapped all the data into the range [0, 6] and the quarterly time to [0, 83]. The result follows:

plot_xts.png

I am sure there are simpler/better solutions. This just serves as an exmaple showing the super flexibility of R graphic.

I have not tried Rsweave but would probably expect little change in the codes. As far as I am aware of there is no limit on number of packages loaded in older version of R.

Here is my session info:
  1. sessionInfo()
  2. # R version 2.15.2 (2012-10-26)
  3. # Platform: i386-w64-mingw32/i386 (32-bit)

  4. # locale:
  5. # [1] LC_COLLATE=English_United States.1252
  6. # [2] LC_CTYPE=English_United States.1252   
  7. # [3] LC_MONETARY=English_United States.1252
  8. # [4] LC_NUMERIC=C                          
  9. # [5] LC_TIME=English_United States.1252   

  10. # attached base packages:
  11. # [1] stats     graphics  grDevices utils     datasets
  12. # [6] methods   base     

  13. # other attached packages:
  14. # [1] xts_0.9-7  zoo_1.7-10

  15. # loaded via a namespace (and not attached):
  16. # [1] grid_2.15.2     lattice_0.20-10 tools_2.15.2
复制代码
Good luck.

藤椅
surah 发表于 2013-11-12 09:52:10
表示顶一下~如果能够帮助解答的可以再追加200论坛币~真的是急求~

板凳
surah 发表于 2013-11-13 14:39:31
zhangyangsmith 发表于 2013-11-12 18:41
I have little knowledge on time series or ananlysis of economic data. But I am interested to do (all ...
Actually what you've posted is virtually the same with the one shown on the stackoverflow or other forums which I've seen before.

I knew the methodology and would take a try. But the major worry is that it is so delicate and arduous to calculate the distance in order to draw the scale of x or y axis. If you have an irregular time series data, it is really difficult and time-exhausted to do that. So that's the reason why I want to find other solutions beyond that and why I choose specific time series packages like xts to draw the time series plot....

报纸
Grace.... 发表于 2020-8-6 22:24:42
不知道这么久过去了楼主解决了这个问题没有。
只要在plot中加上ylim=c(a,b)就可以。其中a b 是你想要的上下限。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-1 20:23