두 쌍의 주식 (쌍 거래)에 대한 분석을 시작했고 여기에 그래프를 작성하기 위해 작성한 함수 (pairs.report - 아래에 나열)가 있습니다.xts 객체를 사용하여 점, 범례 및 텍스트를 플롯에 추가
한 줄에 세 개의 다른 줄을 그려야합니다. 필자가 나열한 함수는 내가 원하는 것을 수행하지만 x 축 (타임 라인)에서 정밀한 사용자 정의를 원한다면 약간의 작업이 필요합니다. 그대로 틱의 형식을 지정하지 않고 단지 x 년 동안 (데이터의 10 년 동안) 또는 개월 (데이터의 6 개월 동안)을 인쇄합니다.
내가
plot(xts-object-with-date-asset1-asset2, ...)
대신
plot(date, asset2, ...)
사용하는 경우 나, 즉 XTS 개체를 사용하는 경우, 나는 그리드와 상자와 함께 (바로 멋지게 형식의 x 축를 얻을 수), points(), text(), lines()과 같은 함수를 사용하여 플롯에 추가하는 것은 실패합니다. 나는 points.xts()와 text.xts()가 곧 나오지 않을 것이라고 생각한다.
xts 객체의 편리함을 원하지만 내 플롯에 대한 세부적인 제어가 필요합니다. 그렇다면 내 작업 흐름은 어떠해야합니까? 기본 그래픽을 고수하고 모든 사용자 정의 작업을 수동으로 수행해야합니까? 아니면 xts가 나를 위해 일할 수있는 방법이 있습니까?
격자와 ggplot2를 알고 있지만 지금은 사용하고 싶지 않습니다. 저는 여기에 언급 된 기능은 (모든 비판은/코드의 개선을위한 제안을 환영합니다) -
library(xts)
pairs.report <- function(asset1, asset2, dataset) {
#create data structures
attach(dataset)
datasetlm <- lm(formula = asset1 ~ asset2 + 0, data = dataset)
beta = coef(datasetlm)[1]
#add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 4) + 0.1)
# Plot first set of data and draw its axis
ylim <- c(min(asset2,asset1), max(asset2,asset1))
plot(date,
asset2,
axes=T,
ylim=ylim,
xlab="Timeline",
ylab="asset2 and asset1 equity",
type="l",
col="red",
main="Comparison between asset2 and asset1")
lines(date, asset1, col="green")
box()
grid(lwd=3)
# Allow a second plot on the same graph
par(new=T)
# Plot the second plot and
ylim <- c(min(asset1-beta*asset2), max(asset1-beta*asset2))
plot(date,
asset1-beta*asset2,
xlab="", ylab="",
ylim=ylim,
axes=F,
type="l",
col="blue")
#put axis scale on right
axis(side=4,
ylim=ylim,
col="blue",
col.axis="blue")
mtext("Residual Spread",side=4,col="blue",line=2.5)
abline(h=mean(asset1-beta*asset2))
}
이 스레드의 내 자신의 질문에 대한 대답은 다음과 같을 수도 있습니다. http://stackoverflow.com/questions/7009711/how-to-get-years-from-a-time-series-index-when -the-underlying-time-series-of-m 최소한 10 년 이상 다른 시간대를 가져야합니다. 물론 진드기도 마찬가지입니다. –
'plot.zoo'는 xts 객체와 작동하며 훨씬 더 세밀한 제어를 제공합니다. '? plot.zoo'에는 수많은 예제가 있습니다. –