<font face="courier new,monospace">Hi --<br></font><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">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. </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">I'll keep you updated.</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">Cheers</font></div><div><font class="Apple-style-span" face="'courier new', monospace">Timothee</font></div><div><div class="gmail_quote">
<br></div><div class="gmail_quote">On Tue, Aug 16, 2011 at 7:26 AM, Matthew Dowle <span dir="ltr"><<a href="mailto:mdowle@mdowle.plus.com">mdowle@mdowle.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Thanks a lot, that makes sense to me now ...<br>
<div class="im"><br>
On Mon, 2011-08-15 at 07:51 +0100, Timothée Carayol wrote:<br>
> You are right -- I should have given a bit more context.<br>
><br>
><br>
> 1) The usual ggplot2 syntax mostly works with data.tables, though<br>
> sometimes coercing to a data.frame is necessary. I do not have a<br>
> reproducible example of that at hand, but I thought it's probably<br>
> something that would require a fix in ggplot2, and not data.table.<br>
> I'll try to find you a reproducible example if that's useful.<br>
<br>
</div>Yes please.<br>
<div class="im"><br>
><br>
> 2) What I wanted to allow, was to call ggplot inside a data.table<br>
> call, e.g.<br>
><br>
><br>
> test <- data.table(<br>
> a=rnorm(4*26),<br>
> b=letters<br>
> )<br>
> test[,<br>
> list(y=max(a)),<br>
> by=b<br>
> ][,<br>
> ggplotDT(x=b, y=y) +<br>
> geom_point() +<br>
> scale_x_discrete("group") +<br>
> scale_y_continuous("maximum") +<br>
> coord_flip()<br>
> ]<br>
><br>
><br>
> I would otherwise have to create an object specifically to contain the<br>
> data to plot, which often I find to be a superfluous step. But maybe<br>
> that's just me, and that's a minor point anyway.<br>
><br>
><br>
> By default, ggplot() (and more specifically aes()) looks only at two<br>
> environments: the global environment, and the data.frame provided as<br>
> its first argument.<br>
<br>
</div>Ah. Is there any possibility of ggplot being changed to look in the<br>
calling frame?<br>
<div class="im"><br>
> That means that with(DF, ggplot(, aes(...)) + ...) doesn't work;<br>
> neither does DT[, ggplot(, aes(...)) + ...]. With aesDT, provided in<br>
> my previous email, these uses become possible.<br>
<br>
</div>Ok these seem like good additions to data.table (assuming ggplot can't<br>
be changed). Does it have to be a different name (aesDT) or could there<br>
be an aes in data.table masking the one in ggplot. Possibly aes could<br>
be internal to data.table, not exported, so user wouldn't realise it's<br>
being masked. Maybe that's a bad thing, though. Views?<br>
<font color="#888888"><br>
Matthew<br>
</font><div><div></div><div class="h5"><br>
><br>
> On Aug 14, 2011 11:34 PM, "Matthew Dowle" <<a href="mailto:mdowle@mdowle.plus.com">mdowle@mdowle.plus.com</a>><br>
> wrote:<br>
> ><br>
> > I'm not a ggplot2 expert by any means but I thought tests 167 and<br>
> 168<br>
> > already tested compatibility. It needs to be called in the following<br>
> > way, though :<br>
> ><br>
> > DT <- data.table(a=1:10, b=1:10)<br>
> > ggplot(DT,aes(x=a,y=b))+geom_line()<br>
> ><br>
> > If you have ggplot2 loaded, then running test.data.table() should<br>
> flash<br>
> > up the ggplot2 plots.<br>
> ><br>
> > Are these forms ok or am I missing something?<br>
> ><br>
> > Matthew<br>
> ><br>
> ><br>
> > On Sun, 2011-08-14 at 21:55 +0100, Timothée Carayol wrote:<br>
> >> Ah sorry -- the definition of ggplotDT() should be, of course:<br>
> >><br>
> >><br>
> >> ggplotDT <- function(...) {<br>
> >> ggplot(, aesDT(...))<br>
> >> }<br>
> >><br>
> >><br>
> >> (aes_now() was an artifact from Hadley Wickham's Stack Overflow<br>
> tip)<br>
> >><br>
> >><br>
> >> t<br>
> >><br>
> >><br>
> >><br>
> >> 2011/8/14 Timothée Carayol <<a href="mailto:timothee.carayol@gmail.com">timothee.carayol@gmail.com</a>><br>
> >> Hi,<br>
> >><br>
> >> For some time I have been wishing that data.table and ggplot2<br>
> >> could play a bit nicer together.<br>
> >> For example, I would like this to work but it doesn't:<br>
> >><br>
> >> > test <- data.table(a=1:10, b=1:10)<br>
> >> > test[, ggplot() + geom_line(aes(x=a, y=b))]<br>
> >> Error in eval(expr, envir, enclos) : object 'a' not found<br>
> >><br>
> >> I spent a few dozen minutes trying to understand what was<br>
> >> going on (and stumbling upon this useful advice from Hadley<br>
> >> Wickham), and it turns out that it's very easy to fix (though<br>
> >> the fix is not very pretty).<br>
> >> Datatable-help, I present you with.. aesDT and ggplotDT.<br>
> >><br>
> >><br>
> >> aesDT <- function(...) {<br>
> >> aes <- structure(list(...), class = "uneval")<br>
> >> rename_aes(aes)<br>
> >> }<br>
> >><br>
> >> Using aesDT() instead of aes() whenever you are in a<br>
> >> data.table (or in a with()) will now work. That is pretty much<br>
> >> all you need, but I thought I'd go just one tiny step further<br>
> >> with ggplotDT():<br>
> >><br>
> >> ggplotDT <- function(...) {<br>
> >> ggplot(, aes_now(...))<br>
> >> }<br>
> >><br>
> >> which makes the following possible:<br>
> >><br>
> >> test[, ggplotDT(x=a, y=b) + geom_line()]<br>
> >><br>
> >> i.e. put the aesthetics directly as arguments, saving a few<br>
> >> keystrokes and a pair of brackets.<br>
> >> Tiny problem that I have yet to solve: you now have to<br>
> >> explicitly give the name of all the aesthetics; aes() is<br>
> >> sufficiently clever to guess them if they're in the right<br>
> >> order, but the logic does not seem to carry over<br>
> >> straightforwardly to aesDT().<br>
> >><br>
> >><br>
> >> I know I'll be using these, so I thought maybe other people<br>
> >> here might be interested.<br>
> >><br>
> >><br>
> >> Timothee<br>
> >><br>
> >><br>
> >> _______________________________________________<br>
> >> datatable-help mailing list<br>
> >> <a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>
> >><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
> ><br>
> ><br>
><br>
<br>
<br>
</div></div></blockquote></div><br></div>