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

Christopher W. Ryan cryan at binghamton.edu
Fri Mar 6 22:03:48 CET 2015


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
plot(density(as.numeric(dd)), axes=FALSE)
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
-- 
Christopher W. Ryan, MD, MS
cryanatbinghamtondotedu

Early success is a terrible teacher. You’re essentially being rewarded
for a lack of preparation, so when you find yourself in a situation
where you must prepare, you can’t do it. You don’t know how.
--Chris Hadfield, An Astronaut's Guide to Life on Earth


---



More information about the R-help mailing list