Values ​​dropped from the ggplot2 histogram when specifying limits

I would like to create a ggplot2 histogram in which the graph limits are equal to the smallest and largest values ​​in the data set, not excluding these values ​​from the actual histogram.

I get the behavior I'm looking for when using basic graphics. In particular, the second histogram below shows all the same values ​​as the first histogram (i.e. no bunkers are excluded on the second histogram), although I included the argument xlimin the second graph:

min_wt <- min(mtcars$wt)
max_wt <- max(mtcars$wt)
xlim <- c(min_wt, max_wt)

hist(mtcars$wt, breaks = 30, main = "No limits added")

hist(mtcars$wt, breaks = 30, xlim = xlim, main = "Limits added")

enter image description here enter image description here

ggplot2 does not give me this behavior:

library(ggplot2)

# Using green colour to make dropped bins easy to see:
p <- ggplot(mtcars, aes(x = wt)) + geom_histogram(colour = "green", bins = 30)
p + ggtitle("No limits added")

p + xlim(xlim) + ggtitle("Limits added") 

enter image description here enter image description here

See how in the second section I lose one of the points that is below 2 and 2 points above 5? I would like to know how to fix this. A few notes:

, boundary, (.. , 2), 2 5, :

ggplot(mtcars, aes(x = wt)) + 
  geom_histogram(bins = 30, colour = "green", boundary = min_wt) + 
  xlim(xlim) +
  ggtitle("Limits added with boundary too")

enter image description here

-, , bins. , bins 50, :

ggplot(mtcars, aes(x = wt)) + 
  geom_histogram(bins = 50, colour = "green", boundary = min_wt) + 
  xlim(xlim) +
  ggtitle("Limits added with boundary too, but with bins = 50")

enter image description here

, , , : geom_histogram: ? : https://github.com/tidyverse/ggplot2/issues/1651. , , " ". ( ): https://github.com/daattali/ggExtra/issues/81.

:

R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] labeling_0.3      colorspace_1.3-2  scales_0.5.0.9000
 [4] compiler_3.4.2    lazyeval_0.2.1    plyr_1.8.4       
 [7] tools_3.4.2       pillar_1.2.1      gtable_0.2.0     
[10] tibble_1.4.2      yaml_2.1.16       Rcpp_0.12.15     
[13] grid_3.4.2        rlang_0.2.0.9000  munsell_0.4.3 
+4
1

, @eipi10 , oob ( ) scale_x_continuous.

, ( ). NA.

scales::censor(), oob = scales::squish, .

.

p + scale_x_continuous(limits = xlim) + ggtitle("default: scales::censor")

: 1 , (geom_bar).

enter image description here

p + scale_x_continuous(limits = xlim, oob = scales::squish) + ggtitle("using scales::squish")

enter image description here

ggplot, , 2 , 5, .

ggplot(mtcars, aes(x = wt)) + 
 geom_histogram(bins = 30, colour = "green", boundary = min_wt) + 
 scale_x_continuous(limits = xlim, oob = scales::squish) +
 ggtitle("Limits added with boundary too") +
 labs(subtitle = "scales::squish")

enter image description here

, .

+1

Source: https://habr.com/ru/post/1694706/


All Articles