How to mark the coordinate of a point on the x & y axis in R

An example would be like this:

enter image description here

The x, y coordinate of point (1, -1) extends along the x, y axis. Right now, I am just adding 2 dashed lines y = 1 and x = -1 with the function xline

and yline

from the package fields

. However, this doesn't work when the point I want to mark is like (0.5, -0.5). Then the corresponding values ​​are not yet included in the axis. In this case, the x axis should be labeled -1, 0, 0.5, 1, 2, 3, but here I am missing 0.5. How to fix it?

Edit: For example, suppose I plotted the parabola y = (x - 0.5) ^ 2 - 0.5

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


How to mark the vertex coordinate like the example in the picture?


source to share

1 answer

You can simply set the x-component and y-components to 0 to get two points, and play with the parameters adj

and pos

to text

to place the text in specific places around the point if you want to label.

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

## 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))


enter image description here



All Articles