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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jan 20 01:08:02 CET 2008


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)

On Jan 19, 2008 5:36 PM, obradoa <aobradovic at gmail.com> wrote:
>
> 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".
>
> Is this possible to do?
>
> Thanks!
>
> Alex
>
> --
> View this message in context: http://www.nabble.com/Advice-on-plotting-a-factor-and-displaying-missing-levels-tp14976118p14976118.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