Learn R Programming

berryFunctions (version 1.11.0)

monthAxis: Label date axis

Description

Labels date axes at sensible intervals in the time domain of weeks to decades.

Usage

monthAxis(side = 1, timeAxis = NA, origin = "1970-01-01", startyear = NULL, stopyear = NULL, n = 5, npm = NULL, npy = NA, format = "%d.%m.\n%Y", labels = format.Date(d, format), ym = FALSE, mcex = 0.6, mmgp = c(3, 0, 0), midyear = FALSE, midmonth = FALSE, midargs = NULL, mgp = c(3, 1.5, 0), cex.axis = 1, tick = TRUE, tcl = par("tcl"), las = 1, ...)

Arguments

side
Which axis are to be labeled? (can be several). DEFAULT: 1
timeAxis
Logical indicating whether the axis is POSIXct, not date. DEFAULT: NA, meaning axis value >1e5
origin
Origin foras.Date and as.POSIXct. DEFAULT: "1970-01-01"
startyear
Integer. starting year. DEFAULT: NULL = internally computed from par("usr")
stopyear
Ditto for ending year. DEFAULT: NULL
n
Approximate number of labels that should be printed (as in pretty). DEFAULT: 5
npm
Number of labels per month, overrides n. DEFAULT: NULL = internally computed.
npy
Number of labels per year, overrides npm and n. DEFAULT: NA
format
Format of date, see details in strptime. DEFAULT: "%d.%m.\n%Y"
labels
labels. DEFAULT: format.Date(d, format)
ym
Label months with first letter at the center of the month and year at center below. Sets midyear and midmonth to TRUE. Uses labels and format for the years, but ignores them for the months. DEFAULT: FALSE
mcex
cex.axis for month labels if ym=TRUE. DEFAULT: 0.8
mmgp
mgp for month labels if ym=TRUE. DEFAULT: 3,0,0
midyear
Place labels in the middle of the year? if TRUE, format default is "%Y". DEFAULT: FALSE
midmonth
Place labels in the middle of the month? if TRUE, format default is "%m\n%Y". DEFAULT: FALSE
midargs
List of arguments passed to axis for the year-start lines without labels. DEFAULT: NULL
mgp
MarGinPlacement, see par. The second value is for label distance to axis. DEFAULT: c(3,1.5,0)
cex.axis
CharacterEXpansion (letter size). DEFAULT: 1
tick
Draw tick lines? DEFAULT: TRUE
tcl
Tick length (negative to go below axis) in text line height units like mgp[2] Changed to -2.5 for year borders if ym=TRUE. DEFAULT: par("tcl")
las
LabelAxisStyle for orientation of labels. DEFAULT: 1 (upright)
...
Further arguments passed to axis, like lwd, col.ticks, hadj, lty, ...

Value

The dates that were labelled

See Also

monthLabs for the numbercrunching itself, axis.Date with defaults that are less nice.

Examples

Run this code

set.seed(007) # for reproducibility
Date1 <- as.Date("2013-09-25")+sort(sample(0:150, 30))
plot(Date1, cumsum(rnorm(30)), type="l", xaxt="n", ann=FALSE)
monthAxis(side=1)
monthAxis(1, npm=2, cex.axis=0.5, col.axis="red") # fix number of labels per month

DateYM <- as.Date("2013-04-25")+0:500
plot(DateYM, cumsum(rnorm(501)), type="l", xaxt="n", ann=FALSE)
monthAxis(ym=TRUE)
monthAxis(ym=TRUE, mgp=c(3,1,0))
monthAxis(ym=TRUE, cex.axis=1.4)
monthAxis(ym=TRUE, mcex=0.9, col.axis="red")
 
plot(Date1, cumsum(rnorm(30)), type="l", xaxt="n", ann=FALSE)
monthAxis(labels=FALSE, col.ticks=2)
monthAxis(1, format=" ")  # equivalent to axis(labels=FALSE)
monthAxis(1)
d <- monthAxis(1, labels=letters[1:24], mgp=c(3,2.5,0))
d # d covers the full year, thus is longer than n=5

Date2 <- as.Date("2011-07-13")+sort(sample(0:1400, 50))
plot(Date2, cumsum(rnorm(50)), type="l", xaxt="n", ann=FALSE)
monthAxis(npy=12, format=" ")  # fix number of labels per year
monthAxis(tcl=-0.8, lwd.ticks=2, format="%Y/%m", mgp=c(3,1,0))
monthAxis(format="", mgp=c(3,2,0)) # International Date format YYYY-mm-dd

plot(Date2, cumsum(rnorm(50)), type="l", xaxt="n", ann=FALSE)
monthAxis(midyear=TRUE)
abline(v=monthLabs(npm=1), col=8)

Date3 <- as.Date("2011-07-13")+sort(sample(0:1200, 50))
plot(Date3, cumsum(rnorm(50)), type="l", xaxt="n", ann=FALSE)
monthAxis(1, n=4, font=2)
monthAxis(1, col.axis=3) # too many labels with default n=5

# mid-year labels:
plot(Date3, cumsum(rnorm(50)), type="l", xaxt="n", ann=FALSE)
monthAxis(midyear=TRUE, midargs=list(tcl=-1.2))

# mid-month labels:
plot(Date1, cumsum(rnorm(30)), type="l", xaxt="n", ann=FALSE)
monthAxis(midmonth=TRUE)

# Time axis instead of date axis:
plot(as.POSIXct(Sys.time()+c(0,10)*24*3600), 1:2, xaxt="n")
monthAxis(n=3)
monthAxis()

Run the code above in your browser using DataLab