简体   繁体   English

如何在 R 中标记 x & y 轴上的点的坐标

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

An example will be like this:一个例子将是这样的:

在此处输入图片说明

The x, y coordinate of point (1, -1) are extended to the x, y axes.点 (1, -1) 的 x, y 坐标扩展到 x, y 轴。 Right now, I am just adding 2 dotted lines y = 1 and x = -1 by function xline and yline from the package fields .现在,我只是通过包fields函数xlineyline添加 2 条虚线 y = 1 和 x = -1 。 However, this does not work when the point I want to mark is something like (0.5, -0.5).但是,当我要标记的点类似于 (0.5, -0.5) 时,这不起作用。 Then the corresponding values are not already included in the axis.那么相应的值尚未包含在轴中。 In this case, the x axis should have label -1, 0, 0.5, 1, 2, 3 but I am missing 0.5 here.在这种情况下,x 轴应该有标签 -1, 0, 0.5, 1, 2, 3 但我在这里缺少 0.5。 How do I fix it?我如何解决它?

Edit: For example, suppose I plotted the parabola y = (x - 0.5)^2 - 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)

How do I mark the coordinate of the vertex like the example in the picture?如何像图片中的示例那样标记顶点的坐标?

You can just set the x-component and y-components to 0 to get the two points, and play with the adj and pos parameters to text to place text in certain locations around the point if you want to label.您只需将 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))

在此处输入图片说明

I found the following solution friend:我找到了以下解决方案的朋友:

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