2012-01-19 3 views
2

일부 가격 대 날짜를 플롯하려고합니다. 약 10k 라인의 데이터가 있습니다. 날짜는 ~ 7/1/2008 - 12/1/2011 범위입니다. 나는 을 위해 각년을 다음 틱과 사용자 정의 X 축을 만들 수 있도록하고 싶습니다 :R 날짜가있는 사용자 정의 x 축을 작성하는 방법

1/1에서

1) YYYY (즉 2011)

2)에서이 4/1 10/1의 달 (즉, Jul)

4)의 약어가 (달의 약어가 7/1의 달 (예 : Apr)

3)의 약어가 즉 Oct)

다음은 데이터에 대한 설명입니다 :

> head(as.POSIXct(rs4p[,3])) 
[1] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" 
[3] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" 
[5] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT" 
> head((rs4p[,3])) 
[1] "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01" 
[6] "2008-07-01" 
> min((rs4p[,3])) 
[1] "2008-07-01" 
> max((rs4p[,3])) 
[1] "2011-12-08" 
> class((rs4p[,3])) 
[1] "Date" 

UPDATE :

깜빡 한가지 그래프가 완료되면 내가 줄거리를 통해 grid()을 넣고 싶은 것입니다. 그리드 라인을 연도 틱에 대응시키는 방법이 있습니까?

+1

업데이트에 요청 된대로 격자 선 코드가 추가되었습니다. –

+0

우수. 고마워요! – screechOwl

답변

3

빈센트 먼저를 얻었으나, 여기 동물원

library(zoo) 
my.ts <-zoo(0:1000,as.Date("2000-01-01")+0:1000) 
plot(my.ts, xaxt="n") 

years <-index(my.ts)[format(index(my.ts),"%m-%d") %in% "01-01"] 
other.quarters <- index(my.ts)[ format(index(my.ts), "%m-%d") %in% c("04-01", "07-01","10-01")] 
axis.Date(1, at=years, label=format(years,"%y")) 
axis.Date(1, at=other.quarters, label=format(other.quarters, "%b")) 

enter image description here

내 버전의를 업데이트 : 눈금 선을 추가하는 방법은 다음과 같습니다.

grid(nx=NA, ny=NULL) 
abline(v=c(years, other.quarters),col = "lightgray", lty = "dotted", lwd = par("lwd")) 

enter image description here

3

axes=FALSE으로 기본 축을 표시하지 않고 직접 axis.Date을 호출하여 추가 할 수 있습니다.

# Sample data 
library(quantmod) 
getSymbols("^DJI") 
x <- DJI 

# Plot without the date axis 
matplot( 
    index(x), coredata(Ad(x)), 
    axes=FALSE, 
    xlab="", ylab="", 
    type="l", lwd=3 
) 
axis(2, las=1) 

# Some date arithmetics 
all_days <- seq.Date(from=min(index(x)), to=max(index(x)), by=1) 
months <- all_days[ format(all_days, "%d") == "01" ] 
january <- all_days[ format(all_days, "%m-%d") == "01-01" ] 
april <- all_days[ format(all_days, "%m-%d") == "04-01" ] 
july <- all_days[ format(all_days, "%m-%d") == "07-01" ] 
october <- all_days[ format(all_days, "%m-%d") == "10-01" ] 

# Finally plot the axes 
axis.Date(1, at=months, label=FALSE, tcl=-.3) 
axis.Date(1, at=january, label=format(january, "%Y")) 
axis.Date(1, at=april, label=format(april, "%b")) 
axis.Date(1, at=july, label=format(july, "%b")) 
axis.Date(1, at=october, label=format(october, "%b")) 

ggplot2 : 날짜의 기본 축은 거의 조정할 필요가 없습니다.

2

빈센트는 나를 이길 수 있지만, 여기에 내가 기본적으로 같은 일 한 방법은 다음과 같습니다

# Dates you want to have in the year format 
fmtasY <- "Jan 01" 

# Dates you want to have in the abbreviated month format (%b) 
fmtasb <- c("Apr 01", "Jul 01", "Oct 01") 

# Some dates 
dates <- as.Date(14061:15309, origin = as.Date("1970-01-01")) 

# Some intermediate labels for formatting 
intermlabs <- format(dates, "%b %d") 

# Set up final labels 
yourlabs <- NA 

# If the date is Jan 01, the label should be %Y 
yourlabs[intermlabs %in% fmtasY] <- format(dates[intermlabs %in% fmtasY], "%Y") 

# If the date is the first of April, July or October, label with %b 
yourlabs[intermlabs %in% fmtasb] <- format(dates[intermlabs %in% fmtasb], "%b") 

# Check your work 
data.frame(dates, intermlabs, yourlabs) 
관련 문제