簡體   English   中英

R:X軸使用對數刻度時在Y軸上繪制點

[英]R : Plotting a point on the Y axis when the X axis is using a log scale

我正在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坐標有關的錯誤。 建議嗎? 一如既往,我感謝這些建議。

這應該起作用,並適當地考慮自己的數據:

# 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

在此處輸入圖片說明

要了解我在這里所做的工作,請看一下?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 (ie, '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坐標,以使其對數(以10底)等於y軸的位置(以用戶坐標表示)。 par("usr")[1]獲取x軸下限的位置: 10^(par("usr")[1]獲取x值,該值將被繪制到x軸的下一部分x軸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM