hadley / gg2v

Render ggplot2 graphics using vega
68 stars 16 forks source link

column name missing in process_stats #3

Closed HarlanH closed 11 years ago

HarlanH commented 11 years ago

Hey Hadley. I'm playing around with gg2v -- cool stuff. Actually planning to do some stuff with Vega in another language for work, but using gg2v to do some quick and dirty proof of concept work. I've found that some simple plots that work fine when rendered via ggplot break in gg2v, seemingly because column names aren't correctly set in aggregation. Here's an example:

> library(plyr)
> library(ggplot2)
> library(gg2v)
> data(baseball)
> p <- ggplot(baseball) + layer(mapping=aes_string(x='lg',y='r'), geom='bar', stat='summary', fun.y='sum')
> p                                 # this works
> gg2v(plot=p)                 # this doesn't
Error in eval(expr, envir, enclos) : object 'y' not found
> options(error=recover)
> gg2v(plot=p)
Error in eval(expr, envir, enclos) : object 'y' not found

Enter a frame number, or 0 to exit   

1: gg2v(plot = p)
2: plot_spec(plot, data_dir = data_dir, ...)
3: plot_data(plot)
4: Map(function(data, aes) render_data(data, aes, env = plot$plot_env), udata,
5: mapply(FUN = f, ..., SIMPLIFY = FALSE)
6: (function (data, aes) 
render_data(data, aes, env = plot$plot_env))(dots[[1]
7: render_data(data, aes, env = plot$plot_env)
8: eval(var, data, env)
9: eval(expr, envir, enclos)

Selection: 3
Called from: eval(var, data, env)
Browse[1]> head(data)
[[1]]
   x group     V1
1        1   2745
2 AA     2   9563
3 AL     3 299992
4 FL     4   1025
5 NL     5 373241
6 PL     6   2739
7 UA     7    300

Browse[1]> head(aes)
$f5f5faf07687c3b68ede8a16ee7e0bce
$f5f5faf07687c3b68ede8a16ee7e0bce$x
x

$f5f5faf07687c3b68ede8a16ee7e0bce$y
y

I'd like to get this fixed for my demos. The issue seems to be in plot-data.R::process_stats, but I'm not exactly sure what needs to be done. Any suggestions? Thanks!

-Harlan

HarlanH commented 11 years ago

FWIW, using this function instead of the convert in your utils.R seems to get around this problem, although I have no idea if it's the preferred solution:

combine2 <- function(x, y) {
  n <- length(x)
  for(i in seq_along(y)) {
    x[[n + i]] <- y[[i]]
    names(x)[[n+i]] <- names(y)[[i]]
  }
  x
}
hadley commented 11 years ago

Hi Harlan,

gg2v is basically just a proof of concept that you could convert ggplot2 into vega, but it's not something I'll be developing further. You're more than welcome to fork and continue working on it though.

Hadley