如何在r图中调整轴从零原点开始

时间:2013-07-02 14:04:45

标签: r

为了绘制三个变量x1,x2和x3的经验累积密度,我在r中使用了以下内容:

plot.ecdf(x1, col="blue",
     main="Distribution XYZ",
     xlab="x_i", ylab="Prob(x_i<=y)")

lines(ecdf(x2), col="red")    # adds a line
lines(ecdf(x3), col="green")  # adds line
legend(600,0.6, # places a legend from (x,y)=(600,0.6) on
       c("x1","x2","x3"), # puts text in the legend
       lty=c(1,1,1), # gives the legend appropriate symbols (lines)
       lwd=c(1,1,1),col=c("blue","red","green")) # gives the legend lines the correct color and width

然而,得到的图除了框之外在0和1处有两条水平线(虚线)。并且,盒子的原点在垂直轴上具有零空间,在水平轴上具有零左侧的空间。您可以建议如何删除这些空间和其他行。我想,但不能发布情节。

EDITED: 可以按如下方式生成样本数据:

样本数据

n <- 1000; u <- runif(n)
a <- -4.46; b <- 1.6; c <- -4.63
d <- ( a * u ) + (b * ( ( 1.5 * ( u ** 2 )) - 0.5 ))  + (c * ( (2.5 * (u ** 3)) - (1.5 * u )))
x1 <- -126/d; x2 <- -131/d; x3 <- -187/d

3 个答案:

答案 0 :(得分:13)

听起来你要求'xaxs'和'yaxs'的不同设置提供的风格,也许'xlim':

尝试:

 plot.ecdf(x1, col="blue",
     main="Distribution XYZ",
     xlab="x_i", ylab="Prob(x_i<=y)", 
     xaxs="i",yaxs="i", xlim=c(0,1000) )

lines(ecdf(x2), col="red")    
lines(ecdf(x3), col="green")  
legend(600,0.6, 
       c("x1","x2","x3"), 
       lty=c(1,1,1), 
       lwd=c(1,1,1),col=c("blue","red","green"))

enter image description here

答案 1 :(得分:8)

使用yaxs = "i"参数:

plot.ecdf(x1, col="blue",
     main="Distribution XYZ",
     xlab="x_i", ylab="Prob(x_i<=y)", yaxs = "i")

这会将轴对齐ylim点(对于plot.ecdf,它应该是0和1)。

如果您还想删除0和1处的虚线,只需拨打box()

box()

答案 2 :(得分:1)

您可以使用参数xlimylimcol.01line

x1 = runif(100)
x2 = runif(100)
x3 = runif(100)
plot.ecdf(x1, col="blue", main="Distribution XYZ",xlab="x_i", ylab="Prob(x_i<=y)", ylim=c(0, 1), xlim=c(0,1), col.01line="white", verticals=FALSE)
lines(ecdf(x2), col="red", col.01line="white")
lines(ecdf(x3), col="green", col.01line="white")
legend(600,0.6,c("x1","x2","x3"), lty=c(1,1,1), lwd=c(1,1,1),col=c("blue","red","green"))  

enter image description here

或者,您可以使用通用plot

f1 = ecdf(x1)
f2 = ecdf(x2)
f3 = ecdf(x3)
pts = seq(0, 1, 0.01)
plot(pts, f1(pts), col="blue", type="b", xlab="x_i", ylab="Prob(x_i<=y)", pch=16, main="Distribution XYZ")
lines(pts, f2(pts), col="red", type="b", pch=16)
lines(pts, f3(pts), col="green", type="b", pch=16)
legend("topleft", c("x1","x2","x3"), fill=c("blue","red","green"))

enter image description here