R quantmod操纵x轴
R quantmod manipulate x-axis
我正在使用 R
和 quantmod
来可视化一些数据
library(quantmod)
# calculate the dates before and after date of interest
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10
# get historical data
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)
# plot this stuff
chartSeries(current_Stock, name = "AAPL", TA = NULL)
# mark day of interest
addTA(xts(TRUE,as.POSIXlt(as.Date(as.character(20101018),"%Y%m%d"))),on=-1, col="gray19")
生成以下图表:
我现在希望 axis
上的日期是垂直的(旋转 90 度)并且没有年份,就像 this thread 的答案一样,然后打印 day of interest
(灰色条所在的位置)在 bold
和 gray
.
中
other thread的结果如下图:
但该解决方案适用于 chart_Series
,我更喜欢 quantmod
中 chartSeries
的设计和着色。
如何制作
- 像第二张图那样的x轴?
- 以粗体和灰色打印灰色条下的日期?
你可以用粗略的方法抑制x轴的到来,error
(据我所知,这是最简单的)和chartSeries(..., TA="add.TA.obj")
(因为addTA()
做一个轴) .然后剩下的就是让轴成为你想要的。
library(quantmod)
### preparation of data (the same as OP's)
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)
### [Edited] preparation of main plot data (I used 2010-10-15 as an interest day)
no_axis <- x <- chartSeries(current_Stock, name = "AAPL", # be careful of ' and " in TA.block
TA = "addTA(xts(TRUE, as.POSIXlt(as.Date('20101015', '%Y%m%d'))), on=-1, col='gray19')", plot=F)
### make plot without x-axis
no_axis@x.labels <- "a" # throw something to x.labels to cause error
quantmod:::chartSeries.chob(no_axis) # The error stops x-axis coming
# try(quantmod:::chartSeries.chob(no_axis), silent = T) # supprres warning version
### make x-axis (I used "x@colors$border" but original is "x@colors$major.tick")
interest <- which(strptime(x@x.labels, "%b %d %Y") == "2010-10-15") # index of the day
par(mar = c(3.5, 3.5, 0, 3)) # I modified lab.position a little by padj.
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[-interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[-interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors$border, col.axis = x@colors$border)
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors$border, col.axis = "gray", font.axis=2)
我正在使用 R
和 quantmod
来可视化一些数据
library(quantmod)
# calculate the dates before and after date of interest
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10
# get historical data
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)
# plot this stuff
chartSeries(current_Stock, name = "AAPL", TA = NULL)
# mark day of interest
addTA(xts(TRUE,as.POSIXlt(as.Date(as.character(20101018),"%Y%m%d"))),on=-1, col="gray19")
生成以下图表:
我现在希望 axis
上的日期是垂直的(旋转 90 度)并且没有年份,就像 this thread 的答案一样,然后打印 day of interest
(灰色条所在的位置)在 bold
和 gray
.
other thread的结果如下图:
但该解决方案适用于 chart_Series
,我更喜欢 quantmod
中 chartSeries
的设计和着色。
如何制作
- 像第二张图那样的x轴?
- 以粗体和灰色打印灰色条下的日期?
你可以用粗略的方法抑制x轴的到来,error
(据我所知,这是最简单的)和chartSeries(..., TA="add.TA.obj")
(因为addTA()
做一个轴) .然后剩下的就是让轴成为你想要的。
library(quantmod)
### preparation of data (the same as OP's)
t1 <- as.Date(as.character(20101018),"%Y%m%d") - 25
t2 <- as.Date(as.character(20101018),"%Y%m%d") + 10
current_Stock <- getSymbols("AAPL", src="yahoo", from=t1, to=t2, auto.assign = FALSE)
### [Edited] preparation of main plot data (I used 2010-10-15 as an interest day)
no_axis <- x <- chartSeries(current_Stock, name = "AAPL", # be careful of ' and " in TA.block
TA = "addTA(xts(TRUE, as.POSIXlt(as.Date('20101015', '%Y%m%d'))), on=-1, col='gray19')", plot=F)
### make plot without x-axis
no_axis@x.labels <- "a" # throw something to x.labels to cause error
quantmod:::chartSeries.chob(no_axis) # The error stops x-axis coming
# try(quantmod:::chartSeries.chob(no_axis), silent = T) # supprres warning version
### make x-axis (I used "x@colors$border" but original is "x@colors$major.tick")
interest <- which(strptime(x@x.labels, "%b %d %Y") == "2010-10-15") # index of the day
par(mar = c(3.5, 3.5, 0, 3)) # I modified lab.position a little by padj.
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[-interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[-interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors$border, col.axis = x@colors$border)
axis(1, at = (1 + x@bp * x@spacing - x@spacing)[interest], padj = 0.4,
labels = (format(strptime(x@x.labels, "%b %d %Y"), "%b %d"))[interest],
las = 2, mgp = c(3, 0.7, 0), col = x@colors$border, col.axis = "gray", font.axis=2)