Geom_histogram: What is the original origin of the first bean?

I would like to know the default start of the first bin in the histogram created using ggplot2for a given cell width. Unfortunately, I did not find any information on the help pages geom_histogram, geom_barand stat_bin. Below is a minimal example for histogram c ggplot2.

 library(ggplot2)
 x <- rnorm(25)
 binwidth <- (range(x)[2]-range(x)[1])/10
 ggplot(data.frame(x=x), aes(x = x)) +
   geom_histogram(aes(y = ..density..), binwidth = binwidth)
+4
source share
1 answer

0, xlimits 0.5*binwidth -0.5*binwidth. width = binwidth , . , > 0, (x+0.5)*binwidth, .

( set.seed ):

set.seed(1)
x <- rnorm(25)
binwidth <- (range(x)[2]-range(x)[1])/10
p <- ggplot(data.frame(x=x), aes(x = x)) +
   geom_histogram(aes(y = ..density..), binwidth = binwidth)

, :

x1 <- ggplot_build(p)$data

:

x1[[1]]$x
 [1] -2.4764874 -2.0954894 -1.7144913 -1.3334932 -0.9524952 -0.5714971 -0.1904990  0.1904990  0.5714971
[10]  0.9524952  1.3334932  1.7144913  2.0954894

, , binwidth + 0.5 (NB, , , ):

binwidth*(floor((min(x)-binwidth/2)/binwidth)+0.5)
-2.476487

:

binwidth*(ceiling((max(x)+binwidth/2)/binwidth)+0.5)
2.095489
+5

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


All Articles