Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
emilelatour opened this issue Oct 30, 2017 · 7 comments

Comments

@emilelatour
Copy link

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     
@ptoche
Copy link

ptoche commented Oct 30, 2017

I'm on ggplot2 v. 2.2.1 and your code is running fine. Just a little message after mtlong:

library(ggplot2)
mtlong <- reshape2::melt(mtcars)
No id variables; using all as measure variables
#> 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)))

rplot

@emilelatour
Copy link
Author

Hmm.... Are you running R on windows or a mac? I've tried it on 3 different windows machines at this point, all running most recent versions of R, RStudio, and ggplot2_2.2.1. I still get
image

@hadley hadley closed this as completed Oct 30, 2017
@hadley
Copy link
Member

hadley commented Oct 30, 2017

You might need to use the devel version; I'm starting to work on the ggplot2 release now

@emilelatour
Copy link
Author

Thank you! That did indeed fix this issue!

When I installed the development version and ran the code again, then things work great! Thank you for such cool features like this!

devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
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)))

image

@jugi92
Copy link

jugi92 commented Jan 24, 2018

This thread helped me and we can use the nclass functions like:

#install.packages("devtools")
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

mtlong <- reshape2::melt(mtcars)

ggplot(mtlong, aes(value)) + facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(binwidth = function(x) (max(x)-min(x))/nclass.FD(x))
ggplot(mtlong, aes(value)) + facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(binwidth = function(x) (max(x)-min(x))/nclass.scott(x))
ggplot(mtlong, aes(value)) + facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(binwidth = function(x) (max(x)-min(x))/nclass.Sturges(x))

@emilelatour
Copy link
Author

Very cool! Your code is so succinct! I ended up with something similar but I also utilized the pretty() base R function which returns "equally spaced 'round' values..." You can see a very clear difference from your code with the variables drat, wt, and carb. Interesting topic about how the choice of binwidth can affect the appearance of histograms.

# devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

mtlong <- reshape2::melt(mtcars)

ggplot(mtlong, aes(value)) +
  facet_wrap(~variable, scales = "free_x") +
  geom_histogram(stat = "bin", binwidth = function(x) {
    pretty(range(x), n = nclass.FD(x), min.n = 1, right = TRUE)[2] - 
      pretty(range(x), n = nclass.FD(x), min.n = 1, right = TRUE)[1]
  })

@lock
Copy link

lock bot commented Jul 23, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jul 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants