jck21
jck21

Reputation: 811

How to mark a specific x value on x-axis without using ggplot?

I have a plot of a quadratic function using the following R code. I wonder if there's a way to mark the value of axis of symmetry on the x-axis (which is the value of vertical line).

b1 = 2
b2 = -1
c =100

x = -10:10
f <- function(x) {
return(c + b1 * x + b2 * x^2) 
}

plot(x, f(x))

abline(v =-b1/(2*b2), col = "red", lwd = 1, lty = 1)

Upvotes: 0

Views: 89

Answers (1)

Quinten
Quinten

Reputation: 41377

Another option is using mtext with as.character to get the value like this:

b1 = 2
b2 = -1
c =100

x = -10:10
f <- function(x) {
  return(c + b1 * x + b2 * x^2) 
}

plot(x, f(x))
abline(v =-b1/(2*b2), col = "red", lwd = 1, lty = 1)
mtext(as.character(-b1/(2*b2)),1,line=1,at=-b1/(2*b2),col="red")

Created on 2022-10-30 with reprex v2.0.2

Upvotes: 1

Related Questions