[R] Advice on plotting a factor and displaying missing levels

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jan 20 04:52:48 CET 2008


Use the zoo package to create a zoo object z which
represents a time series with the tabulated days as
values at the corresponding Date values.  Merge that
with a zero width series containing all the dates from
the start to the end using a fill of 0.  Now plot that using
plot.zoo .

f <- factor(c("10/17/07", "10/17/07", "10/17/07", "10/17/07", "10/17/07",
    "10/17/07", "01/09/08", "01/17/08", "01/17/08", "01/17/08"))

library(zoo)
ndays <- table(f)
z <- zoo(c(ndays), as.Date(names(ndays), "%m/%d/%y"))
z0 <- zoo(, seq(start(z), end(z), "day"))
zz <- merge(z, z0, fill = 0)
plot(zz)

See ?zoo, ?merge.zoo, ?plot.zoo
and
vignette("zoo")
vignette("zoo-quickref")

On Jan 19, 2008 10:25 PM, obradoa <aobradovic at gmail.com> wrote:
>
> Great, this would work. Is it also possible to plot zero values for each
> missing day, so when I do the line graph it properly drops to 0 if there was
> no data for that day? Currently the line graph connects dots that represent
> days with data present, but the line graph never drops to 0. I'd appreciate
> if you have any suggestions for this.
>
> Thanks for you,
>
> Alex
>
>
> Gabor Grothendieck wrote:
> >
> > Tabulate using table(), convert the dates to Date class and plot:
> >
> > f <- factor(c("10/17/07", "10/17/07", "10/17/07", "10/17/07", "10/17/07",
> > "10/17/07", "01/09/08", "01/17/08", "01/17/08", "01/17/08"))
> >
> > ndays <- table(f)
> > plot(as.Date(names(ndays), "%m/%d/%y"), ndays)
> >
>
> >> I am trying to plot how many records are inserted into a database on a
> >> certain date, but also represent days where no records are inserted. I
> >> can
> >> get a list of dates for inserted records using RMySQL
> >>
> >> result <- dbSendQuery(con, "select date_format(creation_ts, "%m/%d/%y")
> >> from
> >> mytable;")
> >> inserts<-fetch(res2, n=-1)
> >> >inserts
> >>    created
> >> 1  10/17/07
> >> 2  10/17/07
> >> 3  10/17/07
> >> 4  10/17/07
> >> 5  10/17/07
> >> 6  10/17/07
> >> 7  01/09/08
> >> 8  01/17/08
> >> 9  01/17/08
> >> 10 01/17/08
> >>
> >>
> >> When I factor the "created" date column I get the following:
> >> > fcreated
> >>  [1] 10/17/07 10/17/07 10/17/07 10/17/07 10/17/07 10/17/07 01/09/08
> >> 01/17/08
> >>  [9] 01/17/08 01/17/08
> >> Levels: 01/09/08 01/17/08 10/17/07
> >>
> >> Then I plot the factor
> >>
> >> plot(fcreated)
> >>
> >> The graph looks correct and I see how many records I have on which date.
> >>
> >> However, I need also to see dates where no records were found in the
> >> database. So instead of having 3 levels 01/09/08 01/17/08 10/17/07, I
> >> need
> >> to have the entire range of dates present on the graph starting with the
> >> earliest date and finishing with the latest date. (For instance 10/17/07,
> >> 10/18/07.... up to the latest date 1/9/08.) Y axis values for those dates
> >> should be 0, except for values tabulated in my factor variable
> >> "fcreated".
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Advice-on-plotting-a-factor-and-displaying-missing-levels-tp14976118p14978080.html
>
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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