我在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