[datatable-help] ggplot2 and data.table
Matthew Dowle
mdowle at mdowle.plus.com
Mon Aug 15 00:34:30 CEST 2011
I'm not a ggplot2 expert by any means but I thought tests 167 and 168
already tested compatibility. It needs to be called in the following
way, though :
DT <- data.table(a=1:10, b=1:10)
ggplot(DT,aes(x=a,y=b))+geom_line()
If you have ggplot2 loaded, then running test.data.table() should flash
up the ggplot2 plots.
Are these forms ok or am I missing something?
Matthew
On Sun, 2011-08-14 at 21:55 +0100, Timothée Carayol wrote:
> Ah sorry -- the definition of ggplotDT() should be, of course:
>
>
> ggplotDT <- function(...) {
> ggplot(, aesDT(...))
> }
>
>
> (aes_now() was an artifact from Hadley Wickham's Stack Overflow tip)
>
>
> t
>
>
>
> 2011/8/14 Timothée Carayol <timothee.carayol at gmail.com>
> Hi,
>
> For some time I have been wishing that data.table and ggplot2
> could play a bit nicer together.
> For example, I would like this to work but it doesn't:
>
> > test <- data.table(a=1:10, b=1:10)
> > test[, ggplot() + geom_line(aes(x=a, y=b))]
> Error in eval(expr, envir, enclos) : object 'a' not found
>
> I spent a few dozen minutes trying to understand what was
> going on (and stumbling upon this useful advice from Hadley
> Wickham), and it turns out that it's very easy to fix (though
> the fix is not very pretty).
> Datatable-help, I present you with.. aesDT and ggplotDT.
>
>
> aesDT <- function(...) {
> aes <- structure(list(...), class = "uneval")
> rename_aes(aes)
> }
>
> Using aesDT() instead of aes() whenever you are in a
> data.table (or in a with()) will now work. That is pretty much
> all you need, but I thought I'd go just one tiny step further
> with ggplotDT():
>
> ggplotDT <- function(...) {
> ggplot(, aes_now(...))
> }
>
> which makes the following possible:
>
> test[, ggplotDT(x=a, y=b) + geom_line()]
>
> i.e. put the aesthetics directly as arguments, saving a few
> keystrokes and a pair of brackets.
> Tiny problem that I have yet to solve: you now have to
> explicitly give the name of all the aesthetics; aes() is
> sufficiently clever to guess them if they're in the right
> order, but the logic does not seem to carry over
> straightforwardly to aesDT().
>
>
> I know I'll be using these, so I thought maybe other people
> here might be interested.
>
>
> Timothee
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
More information about the datatable-help
mailing list