<div class="gmail_quote">You are right -- I should have given a bit more context.</div><div class="gmail_quote"><br></div><div class="gmail_quote">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.</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">2) What I wanted to allow, was to call ggplot inside a data.table call, e.g. </div><div class="gmail_quote"><br></div><div class="gmail_quote">test <- data.table(</div>
<div class="gmail_quote"> a=rnorm(4*26), </div><div class="gmail_quote"> b=letters</div><div class="gmail_quote"> )</div><div class="gmail_quote">test[,</div><div class="gmail_quote">
list(y=max(a)), </div><div class="gmail_quote"> by=b</div><div class="gmail_quote">][,</div><div class="gmail_quote"> ggplotDT(x=b, y=y) + </div><div class="gmail_quote"> geom_point() +</div><div class="gmail_quote">
scale_x_discrete("group") +</div><div class="gmail_quote"> scale_y_continuous("maximum") +</div><div class="gmail_quote"> coord_flip()</div><div class="gmail_quote">]</div><div class="gmail_quote">
<div class="gmail_quote"><br></div><div class="gmail_quote">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.</div>
<div class="gmail_quote"><br></div><div>By default, ggplot() (and more specifically aes()) looks only at two environments: the global environment, and the data.frame provided as its first argument. 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.</div>
</div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Aug 14, 2011 11:34 PM, "Matthew Dowle" <<a href="mailto:mdowle@mdowle.plus.com" target="_blank">mdowle@mdowle.plus.com</a>> wrote:<br type="attribution">
> <br>> I'm not a ggplot2 expert by any means but I thought tests 167 and 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 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 tip)<br>
>> <br>>> <br>>> t<br>>> <br>>> <br>>> <br>>> 2011/8/14 Timothée Carayol <<a href="mailto:timothee.carayol@gmail.com" target="_blank">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" target="_blank">datatable-help@lists.r-forge.r-project.org</a><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></div>