簡體   English   中英

如何在 R 中標記 x & y 軸上的點的坐標

[英]How do I mark the coordinate of a point on x & y axis in R

一個例子將是這樣的:

在此處輸入圖片說明

點 (1, -1) 的 x, y 坐標擴展到 x, y 軸。 現在,我只是通過包fields函數xlineyline添加 2 條虛線 y = 1 和 x = -1 。 但是,當我要標記的點類似於 (0.5, -0.5) 時,這不起作用。 那么相應的值尚未包含在軸中。 在這種情況下,x 軸應該有標簽 -1, 0, 0.5, 1, 2, 3 但我在這里缺少 0.5。 我如何解決它?

編輯:例如,假設我繪制了拋物線 y = (x - 0.5)^2 - 0.5

quadratic <- function (x) {
    return((x - 0.5)^2 - 0.5)
}
curve(quadratic, from = -1, to = 2)

如何像圖片中的示例那樣標記頂點的坐標?

您只需將 x-component 和 y-components 設置為 0 即可獲得兩個點,並使用adjpos參數設置text以將文本放置在該點周圍的特定位置(如果您想標記)。

## Your setup
curve(-(x-1)^2-1, ylim=c(-5,0), xlim=c(-1, 3))
abline(h=0, v=0, lwd=2)
grid()

## Add a point
p <- c(1, -1)
points(t(p), pch=16)
text(t(p), "Vertex", adj=-1)

## At axes
ps <- diag(2)*p  # get points at axes
points(ps, col="red", pch=c("|", "-"), cex=1:2)
text(ps, col="black", labels=paste(diag(ps)), pos=c(1, 4))

在此處輸入圖片說明

我找到了以下解決方案的朋友:

n = -3:3
f = 2^n
plot(n, f, main="Função Exponencial", xlab="X-axis label", ylab="y-axix label", t='l', ylim=c(0,10), xlim=c(-3,3), col=4, axes=F)
axis(1, pos=0)
axis(2, pos=0)   
# Inclui linhas de grade
abline(h=seq(-2,10,0.5),v=seq(-3,3,0.5),lty=3,col="gray", lwd=2)
p <- c(1, 2)
points(t(p), col="red", pch=16)
text(t(p), "Vertex1", adj=-1)
p <- c(2, 4)
points(t(p), col="blue", pch=16)
text(t(p), "Vertex2", adj=-1)

在此處輸入圖片說明

暫無
暫無

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

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