[R] plotting the one-dimensional density of events in time

Tom Wright tom at maladmin.com
Fri Mar 6 22:48:58 CET 2015


plot(density(as.numeric(dd)), 
	axes=FALSE, 
	xlim=c(as.numeric(as.Date("2014-01-01")),
	as.numeric(as.Date("2014-12-30")))
rug(as.numeric(dd))
axis(side=1,
    at=seq(from=as.numeric(as.Date('2014-01-01')),
           to=as.numeric(as.Date('2014-12-1')),length.out=12),
    labels=month.abb,
    las=2)




On Fri, 2015-03-06 at 16:03 -0500, Christopher W. Ryan wrote:
> I have the dates of occurence of a repetitive event. I would like to
> plot the density of these events, as well as their specific temporal
> location.  This is as far as I have gotten:
> 
> # generate some sample data: dates in 2014
> random.dates <- sample(1:31, 100, replace=TRUE)
> random.months <- sample(1:12, 100, replace=TRUE)
> dd <- as.Date(as.character((paste(random.dates, random.months, "2014",
> sep="-"))), format="%d-%m-%Y")
> dd <- dd[!is.na(dd)]
> 
> # plot density with a "rug".
> density(as.numeric(dd))
> plot(density(as.numeric(dd)))
> rug(as.numeric(dd))
> 
> # But horizontal axis label is not very informative
> # would prefer labeling the start of each month

> library(zoo)
> new.axis <- as.yearmon(dd)
> 
> # but then what? This is where I get stuck--adding back a sensible axis
> 
> Grateful for any guidance.
> 
> Thanks.
> 
> --Chris



More information about the R-help mailing list