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

Christopher W. Ryan cryan at binghamton.edu
Mon Mar 9 18:28:47 CET 2015


Tom and Bill--

Thanks! Both excellent solutions.

--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

William Dunlap wrote:
> You could change the x component of density's output back into a Date object
> and let plot choose a Date axis in its usual way.  E.g.,
>   > den <- density(as.numeric(dd))
>   > den$x <- as.Date(den$x, origin=as.Date("1970-01-01"))
>   > plot(den$x, den$y)
> (You probably will also want to normalize the y component to be on a
> specific
> per time unit, say day or year, basis.)
> 
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
> 
> On Fri, Mar 6, 2015 at 1:03 PM, Christopher W. Ryan
> <cryan at binghamton.edu <mailto:cryan at binghamton.edu>> 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 <http://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
> 
> 
>     ---
> 
>     ______________________________________________
>     R-help at r-project.org <mailto:R-help at r-project.org> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     and provide commented, minimal, self-contained, reproducible code.
> 
> 


---



More information about the R-help mailing list