Skip to content

geom_histogram with binwidth function: Example from tidyverse website not working #2312

@emilelatour

Description

@emilelatour

I am trying to run an example that can be found on one of the tidyverse webpages on histograms: http://ggplot2.tidyverse.org/reference/geom_histogram.html

I am trying to use this example to have a function define the binwidth for a histogram. Here is the code example from this link

# You can specify a function for calculating binwidth,
# particularly useful when faceting along variables with
# different ranges
mtlong <- reshape2::melt(mtcars)
#> No id variables; using all as measure variables
ggplot(mtlong, aes(value)) + facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)))

When I run the last command, I get an empty facetted plot (i.e. no histograms) and warnings(). Here is a snippet from the Console:

There were 11 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: Computation failed in `stat_bin()`:
is.numeric(width) is not TRUE
2: Computation failed in `stat_bin()`:
is.numeric(width) is not TRUE
3: Computation failed in `stat_bin()`:
is.numeric(width) is not TRUE

I raise this as an issue here hoping for a solution, but also to make sure that you are aware of the example also showing on the tidyverse website.

Thanks!
Emile

Session Info:

R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] OIsurv_0.2      KMsurv_0.1-5    broom_0.4.2     survival_2.41-3 dplyr_0.7.4     purrr_0.2.4     readr_1.1.1    
 [8] tidyr_0.7.2     tibble_1.3.4    ggplot2_2.2.1   tidyverse_1.1.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13       lubridate_1.7.0    lattice_0.20-35    assertthat_0.2.0   rprojroot_1.2     
 [6] digest_0.6.12      psych_1.7.8        R6_2.2.2           cellranger_1.1.0   plyr_1.8.4        
[11] backports_1.1.1    MatrixModels_0.4-1 evaluate_0.10.1    httr_1.3.1         rlang_0.1.2       
[16] lazyeval_0.2.1     readxl_1.0.0       minqa_1.2.4        SparseM_1.77       extrafontdb_1.0   
[21] car_2.1-5          nloptr_1.0.4       Matrix_1.2-11      rmarkdown_1.6      labeling_0.3      
[26] splines_3.4.2      extrafont_0.17     lme4_1.1-14        stringr_1.2.0      foreign_0.8-69    
[31] munsell_0.4.3      compiler_3.4.2     modelr_0.1.1       janitor_0.3.0      pkgconfig_2.0.1   
[36] base64enc_0.1-3    mnormt_1.5-5       mgcv_1.8-22        htmltools_0.3.6    nnet_7.3-12       
[41] MASS_7.3-47        grid_3.4.2         Rttf2pt1_1.3.4     nlme_3.1-131       jsonlite_1.5      
[46] gtable_0.2.0       pacman_0.4.6       magrittr_1.5       scales_0.5.0       stringi_1.1.5     
[51] reshape2_1.4.2     bindrcpp_0.2       xml2_1.1.1         tools_3.4.2        forcats_0.2.0     
[56] glue_1.2.0         hms_0.3            parallel_3.4.2     pbkrtest_0.4-7     yaml_2.1.14       
[61] colorspace_1.4-0   rvest_0.3.2        knitr_1.17         bindr_0.1          haven_1.1.0       
[66] quantreg_5.34     

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions