ggvis
ggvis copied to clipboard
Some columns not available in ggvis function?
I tried the following, which I expected to work:
iris %>%
ggvis(
~Sepal.Length,
~Sepal.Width,
fill = ~Species,
stroke = ~Petal.Width
) %>%
layer_points() %>%
layer_smooths()
But I end up with this error:
Error in eval(expr, envir, enclos) : object 'Petal.Width' not found
This surprised me since ~Species does appear to work.
I now realise this error is coming from layer_smooths()
- we need a better error message here. Maybe list available vars?
That would certainly help, although I'd be happy just knowing it came from layer_smooths in the error message.
I got similar error with layer_bars(). Here is the example:
> ToothGrowth %>% ggvis(x = ~len, y = ~dose, fill = ~supp) %>% layer_bars()
Error in eval(expr, envir, enclos) : object 'supp' not found
Ying
I'm having the same issue as above and I think this may be related to data.table, dplyr and grouping. In another data, where I've only tried to plot 20 out of 22 categories, I've had some success with ungroup() and regrouping, to get the histogram to show up, but I all but one factor. Happy to dig further if you point me to the right place.
dt = data.table(cust=rep(c("cust1","cust2","cust3"),each=3), category=rep(c("q1","q2","q3"), 3, each=4), val=1:4) dt$category <- factor(dt$category)
z <- dt %>% group_by(cust, category) %>% mutate(total=sum(n()))
str(z)
Classes 'grouped_dt', 'tbl_dt', 'tbl', 'tbl_dt', 'tbl', 'data.table' and 'data.frame': 36 obs. of 4 variables: $ cust : chr "cust1" "cust1" "cust1" "cust2" ... $ category: Factor w/ 3 levels "q1","q2","q3": 1 1 1 1 2 2 2 2 3 3 ... $ val : int 1 2 3 4 1 2 3 4 1 2 ... $ total : int 4 4 4 4 4 4 4 4 4 4 ...
- attr(*, ".internal.selfref")=
- attr(*, "vars")=List of 1 ..$ : symbol cust
Adding/removing the group by does not change the behavior. Also, refactoring category via mutate before calling ggvis doens't alter the behavior either.
z %>% group_by(category) %>% ggvis(x=~total, fill=~factor(category)) %>% layer_histograms(opacity:=1/2, stack=FALSE, width=25)
Error below: Error in factor(category) : object 'category' not found
Below, not exactly the grouping I'm looking for but finding all the variables
g <- ggplot(data=z, aes(y=total, x=cust, fill=category)) + geom_histogram(stat="identity")
R details below
sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-redhat-linux-gnu (64-bit)
locale: [1] C
attached base packages: [1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggvis_0.4 doMC_1.3.3 iterators_1.0.7 foreach_1.4.2
[5] caret_6.0-41 ggplot2_1.0.0 lattice_0.20-29 RColorBrewer_1.1-2
[9] dplyr_0.4.1 magrittr_1.5 lubridate_1.3.3 stringr_0.6.2
[13] data.table_1.9.4
loaded via a namespace (and not attached):
[1] BradleyTerry2_1.0-5 DBI_0.3.1 MASS_7.3-35
[4] Matrix_1.1-4 R6_2.0.1 RJSONIO_1.3-0
[7] Rcpp_0.11.3 assertthat_0.1 brglm_0.5-9
[10] car_2.0-22 chron_2.3-45 codetools_0.2-9
[13] colorspace_1.2-4 digest_0.6.8 grid_3.1.2
[16] gtable_0.1.2 gtools_3.4.1 htmltools_0.2.6
[19] httpuv_1.3.2 jsonlite_0.9.14 lazyeval_0.1.10.9000
[22] lme4_1.1-7 memoise_0.2.1 mime_0.2
[25] minqa_1.2.4 munsell_0.4.2 nlme_3.1-118
[28] nloptr_1.0.4 nnet_7.3-8 plyr_1.8.1
[31] proto_0.3-10 reshape2_1.4.1 scales_0.2.4
[34] shiny_0.11 splines_3.1.2 tools_3.1.2
[37] xtable_1.7-4
So... late to this party but I had a similar issue as @cj-wilson and the (it appears) conflict with data.table (which is rather unfortunate).
Once I had the data I needed removed the inheritance of data.table
with class(foo) <- "data.frame"
which is fugly %>% band-aid %>% badCodeR but it works.
Just putting this out there in case someone else is jonesing for a fix that isn't ggplot2.
Any news about this error?
I actually tried something like:
iris %>% ggvis(x= ~Sepal.Length, y = ~Sepal.Width, fill=~Sepal.Length) %>% layer_bars()
and it doesn't work as well. However it works perfectly with layer_points.
adding a group_by(Sepal.Length) makes it work. It seems like layer_points does this implicitly. I.e. a point can only have one color (group), whereas a bar can be divided in parts.
iris %>%
ggvis(x= ~Sepal.Length, y = ~Sepal.Width, fill=~Sepal.Length) %>%
group_by(Sepal.Length) %>%
layer_bars()