hrbrmstr / metricsgraphics

:chart_with_upwards_trend: htmlwidget interface to the MetricsGraphics.js D3 chart library
http://hrbrmstr.github.io/metricsgraphics/
Other
132 stars 35 forks source link

Allow explicit variable names in mjs_plot #18

Closed jemus42 closed 9 years ago

jemus42 commented 9 years ago

Consider the following:

I have a data.frame and I want to use one variable as the y axis and every other column in the data.frame as the x-axis for a grid of plots based on said data.frame.

I was hoping for something like this to work:

plots <- lapply(names(generalstats[-1]), function(col){
            generalstats %>%
            mjs_plot(x=col, y=player) %>%
              mjs_bar()
          })
mjs_grid(plots, nrow=13, ncol=4)

With generalstats looking something like this:

> head(generalstats)
      player climbOneCm damageDealt mobKills boatOneCm damageTaken   jump leaveGame deaths fishCaught
Jemus42    2415375      802543    17040   1558130      112780  87755       295     37         18
BenemitC     130339      135208     8748     76694      127204  15806        55      4         24
L3viathan    2832879     1083695    37449    131341     1188400 355807       788     76        117
Fenhl    6928955     1849623    95270   2983399      993163 328840      1386     85       3324
Farthen    1295570      222548     8831         0      143204 126832       267     99         10
viirus42      44785         633        5         0       18808    805         2      8          0

If the values for x and y were not supposed to be barebone column names, this would work.
Another way would be for mjs_plot to accept explicit vectors of data for each x and y, without even having to supply a data.frame in the first place.

pandurang-kolekar commented 9 years ago

I also have the same issue. It would be great if mjs_plot() could process the r variables instead of fixed bare names of the variables.

hrbrmstr commented 9 years ago

I just pushed 0.7.5 which enables accessor parameters in functions to be either bare or quoted (i.e. modified mjs_plot, mjs_point&mjs_add_line`). I made a few local tests and it seems to work but if y'all could give it a whirl and let me know that'd be cool.

jrowen commented 9 years ago

I would also agree with the earlier comments and would find it very useful if the code chunk below worked (this is similar to ggplot2::aes_string). I can confirm that mjs_plot(x = "wt", y = "mpg") now works.

x = "wt"
y = "mpg"

mtcars %>%
  mjs_plot(x=x, y=y) %>%
  mjs_point(color_accessor=carb, size_accessor=carb) %>%
  mjs_labs(x="Weight of Car", y="Miles per Gallon")
pandurang-kolekar commented 9 years ago

@hrbrmstr I also confirmed that the 0.7.5 version works fine with quoted strings. But as described correctly by @jrowen the mjs_plot should process R objects as x and y variables as shown in his previous comment.

birdplane-industries commented 9 years ago

This issue has still not been fixed, and I believe it is a very important feature to add for use in Shiny Apps in which the user chooses what fields to plot. And I don't think a fix would be too difficult. I think the real problem is the use of substitute() inside mjs_plot(). This seems to be a dangerous function because it forces any input to be an expression. It will force a variable name to be the character expression of that variable name--not the character string that the variable actually represents. And I see no way around it.

As @jrowen mentions, we should be able to just reverse engineer how ggplot2's aes_string() handles its input parameters.

aes_string <- function (x = NULL, y = NULL, ...) 
{
    mapping <- c(compact(list(x = x, y = y)), list(...))
    mapping[vapply(mapping, is.null, logical(1))] <- "NULL"
    parsed <- lapply(mapping, function(x) parse(text = x)[[1]])
    structure(rename_aes(parsed), class = "uneval")
}
jrowen commented 9 years ago

Recently, I ran into a similar issue with the rbokeh library, so I made an attempt at a fix here. If you have a minute, please give it a try an let me know if it works for you.

devtools::install_github("jrowen/metricsgraphics", ref = "allow_vrbl_names")
hrbrmstr commented 9 years ago

@jrowen I shall, but in the meantime folks can try the "mjsplot" version in the dev branch - https://github.com/hrbrmstr/metricsgraphics/tree/dev01

I also added an as_mjsplot function to take simple ggplot objects and convert them to metricsgraphics ones