ggplot 直方图相对于轴的位置不正确
ggplot histogram is not in the correct position with respect the axis
我试图用这种方式绘制直方图:
# 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
结果是:
但是如你所见,y轴和直方图之间有一个space。我怎样才能移动直方图以使其正确定位?
要使直方图与 y 轴对齐,您可以将以下代码行添加到绘图中:"boundary = 0"
边界和中心都是 bin 位置说明符。有关详细信息,我已经粘贴了 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."
在这种情况下,通过指定 boundary = 0,您可以强制 bin 位置与图形的原点 (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
现在你的情节应该是这样的:
我试图用这种方式绘制直方图:
# 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
结果是:
但是如你所见,y轴和直方图之间有一个space。我怎样才能移动直方图以使其正确定位?
要使直方图与 y 轴对齐,您可以将以下代码行添加到绘图中:"boundary = 0"
边界和中心都是 bin 位置说明符。有关详细信息,我已经粘贴了 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."
在这种情况下,通过指定 boundary = 0,您可以强制 bin 位置与图形的原点 (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
现在你的情节应该是这样的: