Question

So I have a plot in R, but the points seem to be offset by a small amount... (maybe by 1 point to the right).

This is my code:

  x <- 0:30
  db<-dbinom(x, 30, 0.30)
  plot(x, db, type = "h", ylab=NULL)
  lines(x, db, type = "l", lty=2, ylab=NULL)
  points(db, y=NULL, col="red")

And here is the plot:

R plot, points offset

Am I calling the points() functions incorrectly or missing something out? I can't find anything on here or google... Any guidance would be appreciated. Thanks in advance.

Was it helpful?

Solution

By default, if you supply no y argument, points() will plot the points at x=1 to x=length(data). To fix that, modify you code as follows:

points(x, y = db, col="red")

OTHER TIPS

You need to set both x and y to align it properly.

Change

points(db, y=NULL, col="red")

To

points(x, db, col="red")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top