[英]Factors in ggvis layer_histograms
I'm having an issue with factor variables in ggvis. 我在ggvis中遇到因子变量问题。 I've added a sample df below that's a mock of my real data.
我在下面添加了一个示例df,它是对我的真实数据的模拟。 Basically I'm trying to get a histogram by customer filled by category.
基本上,我正在尝试按类别填充客户的直方图。 At the end of the dplyr pipeline I have "cust" and "total" events by category, what I get is an error on the "cust" factor.
在dplyr管道的末尾,我按类别具有“ cust”和“ total”事件,我得到的是“ cust”因子上的错误。 I think this is a grouping issue, so my example contains code I've tried that's commented out and some additional color for my problem.
我认为这是一个分组问题,因此我的示例包含已尝试将其注释掉的代码以及该问题的其他颜色。 Thanks in advance.
提前致谢。
Example data frame 示例数据框
df = data.frame(cust=rep(c("cust1","cust2","cust3"),each=3),
category=rep(c("q1","q2","q3"), 3, each=4),
val=1:4)
If I comment out the group/ungroup statements I get a factor range error un-commenting the x=~total line plots a single bar filled properly. 如果我注释掉group / ungroup语句,则会得到因子范围错误,无法注释x =〜total线图,正确填充了单个条形。 Wrong, but creates the geom none the less.
错了,但是仍然创造了几何。
df %>% group_by(cust, category) %>%
summarise(total=sum(n())) %>%
ungroup() %>%
select(cust, category, total) %>%
group_by(category) %>%
ggvis(x=~cust, fill=~category) %>%
#ggvis(x=~total, fill=~category) %>%
layer_histograms(opacity:=1/2, stack=TRUE, width=2)
Error in Summary.factor(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), na.rm = FALSE) : 'range' not meaningful for factors
Here is an equivalent plot in ggplot2 that is what I think I'm looking for. 这是ggplot2中的等效图,这是我想寻找的图。 I've left out all of the group/ungroup lines I was using for debugging above.
我上面已经省略了用于调试的所有分组/取消分组行。
g <- ggplot(data=df %>% group_by(cust, category) %>%
summarise(total=sum(n())), aes(y=total, x=cust, fill=category))
g + geom_histogram(stat="identity")
Session info 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
I was able to dig into this a little deeper today and have a temporary fix for anyone who has the same issue. 今天,我能够对此进行更深入的研究,并为遇到相同问题的任何人提供一个临时解决方案。 Since some of the examples on the RStudio site are broken as well, I figure someone's going to run into this.
由于RStudio网站上的一些示例也已损坏,因此我认为有人会遇到这种情况。
If you put a trace on the previous call to ggvis
it breaks where the bins are computed in compute_bin.R
using a custom range function. 如果在上一个对
ggvis
调用上放置跟踪,则会中断使用自定义范围函数在compute_bin.R
中计算bin的位置。 Since you can't call range()
on a factor it breaks right there. 由于您不能在一个因数上调用
range()
,因此会在此中断。 I've submitted a request for a fix on GitHub, but the temp fix is to unclass()
the factor during the call to ggvis as I've shown below. 我已经在GitHub上提交了修复请求,但是临时修复是在调用ggvis的过程中
unclass()
对unclass()
的因素,如下所示。
Hope this helps. 希望这可以帮助。
z %>%
ggvis(x=~unclass(cust), fill=~category) %>%
layer_histograms(opacity:=1/2, stack=TRUE, width=0.5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.