Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put infinity and -infinity on a graph with plot

In the attached image I want to replace -1 by -Inf and 3 by Inf.

enter image description here

This is the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

F <- function(x) {
  f <- NULL
  f[x < 0] <- 0
  f[x >= 0 & x < 1] <- 0.4*x[x >= 0 & x < 1]^2
  f[x >= 1 & x < 2] <- 0.2*x[x >= 1 & x < 2]^2 + (0.2)
  f[x >= 2] <- 1
  f
}
x <- seq(-1, 3, 0.01)
plot(x, F(x), type="l", las=1)

>Solution :

Use xaxt='n' to omit x-axis. axTicks(1) gives you current tick positions; you can replace first and last element and replace them with custom stuff.

plot(x, F(x), type="l", las=1, xaxt='n')
at1 <- axTicks(1)
at1[c(1, length(at1))] <- c('-Inf', 'Inf')
axis(1, at=axTicks(1), labels=at1)

enter image description here

You could also use the symbols using bquotes, which you’d need to provide as.expression.

plot(x, F(x), type="l", las=1, xaxt='n')
atl <- axTicks(1)
atl[c(1, length(at))] <- sapply(c(bquote(-infinity), bquote(infinity)), as.expression)
axis(1, at=axTicks(1), labels=atl)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading