R:当X轴使用对数刻度时,在Y轴上绘制一个点

时间:2012-02-13 18:08:23

标签: r plot

我在R中制作一个情节我想在y轴上的某个特定点(例如,alpha)上出现一个符号。我的x轴使用对数刻度,所以我不认为我可以使用xy坐标(0,alpha)来绘制点(我可能错了)。

我尝试过这样的事情:

W.range <- range(W)
Z.range <- range(Z1, Z2)
# Draw Empty Graph 
plot(W.range, Z.range, type="n", log="x")
# First Curve
lines(W, Z1, type="l", lty=1, lwd=1.5, col=1)
# Second Curve
lines(W, Z2, type="l", lty=2, lwd=1.5, col=2)
# Plot Single Point on vertical axis at level alpha (for some constant alpha)
points(x=0, y=alpha, pch=1, col=1)

但是,我得到与x = 0坐标有关的错误。建议吗?一如既往,我很欣赏这些建议。

1 个答案:

答案 0 :(得分:5)

这应该有效,在适当的时候用你自己的数据进行补充:

# Example data and plot
x <- y <- 1:100
alpha <- 44
plot(x,y, log="x", type="l")    

# Add point
points(x = 10^(par("usr")[1]), 
       y = alpha, 
       pch = 16, cex=1.3, col = "red", las = 1,
       xpd = TRUE) # controls whether or not pts straddling the axis are clipped

enter image description here

要了解我在这里所做的事情,请查看?par,我在这里使用它来查询和设置图形参数。

特别是,以下是上面链接的帮助文件中par("usr")的说明:

‘usr’ A vector of the form ‘c(x1, x2, y1, y2)’ giving the extremes
      of the user coordinates of the plotting region.  When a
      logarithmic scale is in use (i.e., ‘par("xlog")’ is true, see
      below), then the x-limits will be ‘10 ^ par("usr")[1:2]’.
      Similarly for the y-axis.

usr返回的x坐标是x的对数变换值。因此,要在y轴上放置一个点,需要给它一个x坐标,使得它的log(基数10)等于y轴的位置,用户表示坐标。 par("usr")[1]可以获得x轴下限的位置:10^(par("usr")[1]可以获得将被绘制到x轴的那一部分的x值。