geom_hexbin将bincount映射到alpha

时间:2016-09-12 09:10:41

标签: r ggplot2

我希望geom_hex bincount为alpha,就像here一样。

不知何故,它对我不起作用,可能出错? (开发版ggplot2?):

library(ggplot2)
library(reshape2)

dm <- melt(diamonds, measure.var = c('depth','carat'))

ggplot(dm, aes(y = price, fill = variable, x = value)) + 
  facet_wrap(~variable, ncol = 1, scales  = 'free_x') + 
  stat_binhex(aes(alpha = ..count..), colour = 'grey80') + 
  scale_alpha(name = 'Frequency', range = c(0,1)) + 
  theme_bw() + 
  scale_fill_manual('Variable', values = setNames(c('darkblue','yellow4'), c('depth','carat')))

Error in eval(expr, envir, enclos) : object 'count' not found

sessionInfo:

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=nl_NL.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=nl_NL.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=nl_NL.UTF-8      
 [8] LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=nl_NL.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] hexbin_1.27.1      reshape2_1.4.1     ggplot2_2.1.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7      lattice_0.20-33  assertthat_0.1   grid_3.2.3       plyr_1.8.4       gtable_0.2.0     magrittr_1.5     scales_0.4.0     stringi_1.1.1    tools_3.2.3      stringr_1.1.0    munsell_0.4.3   
[13] rsconnect_0.4.3  colorspace_1.2-6 tibble_1.2 

1 个答案:

答案 0 :(得分:2)

从那时起,包中发生了一些变化,这些变化改变了..count....density..hexbin的合作方式。 @RichardTelford的链接指向..density..已解决的github问题,但..count..功能尚未恢复。但是,我们可以简单地使用..value..

ggplot(dm, aes(y = price, fill = variable, x = value)) + 
  facet_wrap(~variable, ncol = 1, scales  = 'free_x') + 
  stat_binhex(aes(alpha = ..value..), colour = 'grey80') + 
  scale_alpha(name = 'Frequency', range = c(0,1)) + 
  theme_bw() + 
  scale_fill_manual('Variable', values = setNames(c('darkblue','yellow4'), c('depth','carat')))

我已将此报告为问题on github

enter image description here