4

I was trying to plot a histogram this way:

# Todo lo haremos con base en un variable aleatoria Uniforme(0,1).
set.seed(26) ; n = 10000
U<-runif(n = n)
# Supongamos que queremos simular de una exponencial.

# Función de distribución: F(X) = 1-exp(-lambda*X) = U
# Entonces, X = F^(-1)(X)= log(1-U)/(-lambda)
lambda = 1/6 # El parámetro de la exponencial que vamos a usar.
X <- log(1-U)/(-lambda)

library(ggplot2)
p <- qplot(X,
           geom="histogram",
           binwidth = 2,  
           main = "Histograma de X", 
           xlab = "Observaciones",  
           # La función "I" hace que no aparezca una descripción.
           fill=I("yellow"), 
           col=I("blue"), 
           alpha=I(0.2),
           xlim=c(0,50))+
  geom_hline(yintercept = 0,col="red",lwd=1)+
  geom_vline(xintercept = 0,col="red",lwd=1)
p

And the result was:

enter image description here

But as you can see, the y axis and the histogram has a space between. How could I move the histogram so that its correctly positioned?

  • Also see stevec's answer here, which is related: https://stackoverflow.com/questions/41486027/ggplot2-how-to-align-the-bars-of-a-histogram-with-the-x-axis – Wu Wei Jun 06 '20 at 02:54

1 Answers1

5

To make the histogram align with the y-axis you can add the following line of code to your plot: "boundary = 0"

Both Boundary and Center are bin position specifiers. For more details, I have pasted the description from the ggplot2 reference guide. "Only one, center or boundary, may be specified for a single plot. Center specifies the center of one of the bins. Boundary specifies the boundary between two bins. Note that if either is above or below the range of the data, things will be shifted by the appropriate integer multiple of width. For example, to center on integers use width = 1 and center = 0, even if 0 is outside the range of the data. Alternatively, this same alignment can be specified with width = 1 and boundary = 0.5, even if 0.5 is outside the range of the data."

In this case, by specifying boundary = 0 you can force the bin position to align with the origin of the graph (0,0).

# Todo lo haremos con base en un variable aleatoria Uniforme(0,1).
set.seed(26) ; n = 10000
U<-runif(n = n)
# Supongamos que queremos simular de una exponencial.

# Función de distribución: F(X) = 1-exp(-lambda*X) = U
# Entonces, X = F^(-1)(X)= log(1-U)/(-lambda)
lambda = 1/6 # El parámetro de la exponencial que vamos a usar.
X <- log(1-U)/(-lambda)

library(ggplot2)
p <- qplot(X,
           geom="histogram",
           binwidth = 2,
           boundary = 0, #This controls the bin alignment with the y-axis
           main = "Histograma de X", 
           xlab = "Observaciones",  
           # La función "I" hace que no aparezca una descripción.
           fill=I("yellow"), 
           col=I("blue"), 
           alpha=I(0.2),
           xlim=c(0,50))+
  geom_hline(yintercept = 0,col="red",lwd=1)+
  geom_vline(xintercept = 0,col="red",lwd=1)
#  geom_histogram(binwidth = 1, boundary = 0, closed = "left")
p

Now your plot should look like this:

enter image description here

Wu Wei
  • 380
  • 1
  • 11