Christopher W. Ryan
2015-Mar-06 21:03 UTC
[R] plotting the one-dimensional density of events in time
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 ---
Tom Wright
2015-Mar-06 21:48 UTC
[R] plotting the one-dimensional density of events in time
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
William Dunlap
2015-Mar-06 22:29 UTC
[R] plotting the one-dimensional density of events in time
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 On Fri, Mar 6, 2015 at 1:03 PM, Christopher W. Ryan <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(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 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.[[alternative HTML version deleted]]
Christopher W. Ryan
2015-Mar-09 17:28 UTC
[R] plotting the one-dimensional density of events in time
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. > >---