[datatable-help] ggplot2 and data.table
Timothée Carayol
timothee.carayol at gmail.com
Tue Aug 16 14:17:04 CEST 2011
Hi --
I am currently at the useR! conference so my time to look into this is a bit
limited -- as is my access to data and code which I remember to have
required a coercion to data.frame in the past. When I have some time I'll
experiment a bit more, before perhaps raising the issues with Hadley Wickham
to see what his thoughts are.
I'll keep you updated.
Cheers
Timothee
On Tue, Aug 16, 2011 at 7:26 AM, Matthew Dowle <mdowle at mdowle.plus.com>wrote:
>
> Thanks a lot, that makes sense to me now ...
>
> On Mon, 2011-08-15 at 07:51 +0100, Timothée Carayol wrote:
> > You are right -- I should have given a bit more context.
> >
> >
> > 1) The usual ggplot2 syntax mostly works with data.tables, though
> > sometimes coercing to a data.frame is necessary. I do not have a
> > reproducible example of that at hand, but I thought it's probably
> > something that would require a fix in ggplot2, and not data.table.
> > I'll try to find you a reproducible example if that's useful.
>
> Yes please.
>
> >
> > 2) What I wanted to allow, was to call ggplot inside a data.table
> > call, e.g.
> >
> >
> > test <- data.table(
> > a=rnorm(4*26),
> > b=letters
> > )
> > test[,
> > list(y=max(a)),
> > by=b
> > ][,
> > ggplotDT(x=b, y=y) +
> > geom_point() +
> > scale_x_discrete("group") +
> > scale_y_continuous("maximum") +
> > coord_flip()
> > ]
> >
> >
> > I would otherwise have to create an object specifically to contain the
> > data to plot, which often I find to be a superfluous step. But maybe
> > that's just me, and that's a minor point anyway.
> >
> >
> > By default, ggplot() (and more specifically aes()) looks only at two
> > environments: the global environment, and the data.frame provided as
> > its first argument.
>
> Ah. Is there any possibility of ggplot being changed to look in the
> calling frame?
>
> > That means that with(DF, ggplot(, aes(...)) + ...) doesn't work;
> > neither does DT[, ggplot(, aes(...)) + ...]. With aesDT, provided in
> > my previous email, these uses become possible.
>
> Ok these seem like good additions to data.table (assuming ggplot can't
> be changed). Does it have to be a different name (aesDT) or could there
> be an aes in data.table masking the one in ggplot. Possibly aes could
> be internal to data.table, not exported, so user wouldn't realise it's
> being masked. Maybe that's a bad thing, though. Views?
>
> Matthew
>
> >
> > On Aug 14, 2011 11:34 PM, "Matthew Dowle" <mdowle at mdowle.plus.com>
> > wrote:
> > >
> > > 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
> > >
> > >
> >
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20110816/d856a908/attachment.htm>
More information about the datatable-help
mailing list