【问题标题】:Factors in ggvis layer_histogramsggvis layer_histograms 中的因素
【发布时间】:2015-02-11 17:41:03
【问题描述】:

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

示例数据框

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

如果我注释掉 group/ungroup 语句,我会得到一个因子范围错误,取消注释 x=~total 线会绘制一个正确填充的单条。错了,但仍然创建了几何图形。

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        

【问题讨论】:

    标签: r histogram dplyr ggvis


    【解决方案1】:

    我今天能够更深入地研究这个问题,并为遇到相同问题的任何人提供临时解决方案。由于 RStudio 网站上的一些示例也被破坏了,我认为有人会遇到这种情况。

    如果您对上一次调用 ggvis 进行跟踪,则会中断使用自定义范围函数在 compute_bin.R 中计算 bin 的位置。由于您不能在某个因素上致电range(),因此它会在那里中断。我已经在 GitHub 上提交了修复请求,但临时修复是针对unclass() 调用 ggvis 期间的因素,如下所示。

    希望这会有所帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2017-10-04
      • 2015-08-04
      • 1970-01-01
      相关资源
      最近更新 更多