Closed jemus42 closed 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.
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.
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")
@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.
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")
}
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")
@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
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:
With
generalstats
looking something like this:If the values for
x
andy
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 eachx
andy
, without even having to supply a data.frame in the first place.