2015-02-11 40 views
1

我在ggvis中遇到因子变量问题。我在下面添加了一个示例df,这是我真实数据的一个模拟。基本上我试图按类别填充客户的直方图。在dplyr管道的末端,我有“cust”和“total”事件的分类,我得到的是“cust”因素的错误。我认为这是一个分组问题,所以我的例子包含了我已经尝试过的代码,这些代码已经被注释掉了,还有一些我的问题的附加颜色。提前致谢。ggvis layer_histograms中的因子

实施例的数据帧

df = data.frame(cust=rep(c("cust1","cust2","cust3"),each=3), 
       category=rep(c("q1","q2","q3"), 3, each=4), 
       val=1:4) 

如果我注释掉的组/取消分组语句我得到一个因数范围错误取消注释在x =〜总线绘出填充适当的单个杆。错了,但创造了几乎没有。

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 

下面是ggplot2中的等效图,这是我认为我在寻找的。我忽略了以上用于调试的所有组/分组行。

g <- ggplot(data=df %>% group_by(cust, category) %>% 
       summarise(total=sum(n())), aes(y=total, x=cust, fill=category)) 
g + geom_histogram(stat="identity") 

下面的会话信息。

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   

回答

0

我今天能够深入了解这一点,并对任何有同样问题的人进行临时修复。由于RStudio网站上的一些示例也被破坏,我认为有人会遇到这种情况。

如果您在前一次调用ggvis时放了一个跟踪,它将使用自定义范围函数在compute_bin.R中计算垃圾箱的位置。既然你不能打电话range()一个因素它就在那里。我已经提交了GitHub上的修复请求,但临时修正是在调用ggvis期间的因素unclass(),如下所示。

希望这会有所帮助。

z %>% 
    ggvis(x=~unclass(cust), fill=~category) %>% 
    layer_histograms(opacity:=1/2, stack=TRUE, width=0.5)