4

An example will be like this:

enter image description here

The x, y coordinate of point (1, -1) are extended to the x, y axes. Right now, I am just adding 2 dotted lines y = 1 and x = -1 by function xline and yline from the package fields. However, this does not work when the point I want to mark is something like (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. How do I 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 do I mark the coordinate of the vertex like the example in the picture?

Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Provide sample input data and show the code you've already tried. – MrFlick Aug 03 '15 at 22:55

2 Answers2

5

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.

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

enter image description here

Rorschach
  • 31,301
  • 5
  • 78
  • 129
0

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)

enter image description here

ricardoramos
  • 891
  • 3
  • 18
  • 35