Producing multiple qplots with data.table
data.table, ggplot2, r
Solution
I'm not 100% sure exactly what's going on, but here is a workaround:
result = dt[, list(plot = list(qplot(data=.SD, A, C))), by = B]
It seems like `data.table` reserves an environment to execute its evaluations in in some way, and `qplot` just stores a reference to this environment. Since you don't specify a `data` argument, `qplot` will look in that environment for the data, which keeps getting changed for each `by` value by `data.table`, so after the `data.table` operation, only the last piece is left there.
Problem
I'd like to produce one scatter plot per group from a data.table and return it from a function. I can produce the different charts using plot (below) so I assume this isn't a data.table issue: ``` dt = data.table(A = c(1,2,3,4), B = c(1,1,2,2), C = c(4,5,6,7)) result = dt[, list(plot = list(plot(A, C))), by = B] ``` However if I try to do the same with qplot (in order to get plots I can return), I appear to end up with two copies of the second chart. ``` dt = data.table(A = c(1,2,3,4), B = c(1,1,2,2), C = c(4,5,6,7)) result = dt[, list(plot = list(qplot(A, C))), by = B] result[1,][["plot"]] result[2,][["plot"]] ``` Apologies if I'm missing something obvious / doing something stupid.