這裏是一個函數,會做基本的每年或每月日曆:
cal <- function(month, year) {
if(!require(chron)) stop('Unable to load chron package')
if(missing(year) && missing(month)) {
tmp <- month.day.year(Sys.Date())
year <- tmp$year
month <- tmp$month
}
if(missing(year) || missing(month)){ # year calendar
if(missing(year)) year <- month
par(mfrow=c(4,3))
tmp <- seq.dates(from=julian(1,1,year), to=julian(12,31,year))
tmp2 <- month.day.year(tmp)
wd <- do.call(day.of.week, tmp2)
par(mar=c(1.5,1.5,2.5,1.5))
for(i in 1:12){
w <- tmp2$month == i
cs <- cumsum(wd[w]==0)
if(cs[1] > 0) cs <- cs - 1
nr <- max(cs) + 1
plot.new()
plot.window(xlim=c(0,6), ylim=c(0,nr+1))
text(wd[w], nr - cs -0.5 , tmp2$day[w])
title(main=month.name[i])
text(0:6, nr+0.5, c('S','M','T','W','T','F','S'))
}
} else { # month calendar
ld <- seq.dates(from=julian(month,1,year), length=2, by='months')[2]-1
days <- seq.dates(from=julian(month,1,year), to=ld)
tmp <- month.day.year(days)
wd <- do.call(day.of.week, tmp)
cs <- cumsum(wd == 0)
if(cs[1] > 0) cs <- cs - 1
nr <- max(cs) + 1
par(oma=c(0.1,0.1,4.6,0.1))
par(mfrow=c(nr,7))
par(mar=c(0,0,0,0))
for(i in seq_len(wd[1])){
plot.new()
#box()
}
day.name <- c('Sun','Mon','Tues','Wed','Thur','Fri','Sat')
for(i in tmp$day){
plot.new()
box()
text(0,1, i, adj=c(0,1))
if(i < 8) mtext(day.name[wd[i]+1], line=0.5,
at=grconvertX(0.5,to='ndc'), outer=TRUE)
}
mtext(month.name[month], line=2.5, at=0.5, cex=1.75, outer=TRUE)
#box('inner') #optional
}
}
您可以編輯各種片段來定製它。您可以使用par(mfg=c(r,c))
向月曆中的給定單元格添加文本,圖表或其他信息(請參閱TeachingDemos
包中的updateusr
和subplot
以獲取可能的添加圖的幫助)。
這裏有添加文字或圖表日曆的一些例子:
cal(10,2011)
par(mfg=c(3,2)) # monday oct 10
text(.5,.5, 'Some\nText', cex=2)
par(mfg=c(2,3)) #Tues oct 4
text(1,1, 'Top Right', adj=c(1,1))
par(mfg=c(2,4)) # Wed oct 5
text(0,0, 'Bottom Left', adj=c(0,0))
par(mfg=c(6,2)) # oct 31
tmp.x <- runif(25)
tmp.y <- rnorm(25,tmp.x,.1)
par(usr=c(range(tmp.x), range(tmp.y)))
points(tmp.x,tmp.y)
如何'系統( 'CAL')'? – kohske
@kohske - 這是操作系統相關的。適用於UNIX,但不適用於Windows ... –
正是如此。這就是爲什麼這是一個評論,而不是一個答案。 – kohske