Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot直方图相对于轴的位置不正确_R_Ggplot2_Histogram_Axis - Fatal编程技术网

R ggplot直方图相对于轴的位置不正确

R ggplot直方图相对于轴的位置不正确,r,ggplot2,histogram,axis,R,Ggplot2,Histogram,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

我试图用这种方式绘制直方图:

# 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
#Todo lo haremos con base en un variable aleatoria制服(0,1)。
结实。种子(26);n=10000

U要使直方图与y轴对齐,可以在绘图中添加以下代码行:“边界=0”

边界和中心都是箱位置说明符。有关更多详细信息,我已从中粘贴了描述。“对于单个绘图,只能指定一个,中心或边界。中心指定其中一个箱子的中心。边界指定两个箱子之间的边界。请注意,如果其中一个箱子高于或低于数据范围,则将按适当的宽度整数倍移动。例如,要以整数为中心,请使用宽度=1和c。”输入=0,即使0在数据范围之外。或者,可以指定相同的对齐方式,宽度=1,边界=0.5,即使0.5在数据范围之外。”

在这种情况下,通过指定boundary=0,可以强制箱子位置与图形原点对齐(0,0)

#Todo lo haremos con base en un variable aleatoria制服(0,1)。
结实。种子(26);n=10000

UAL也可以在这里看到stevec的答案,这是相关的:
# 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