Axis position in R scatterplot
I'm trying to create a simple scatter plot in R, where the x-axis range is -10:10, and to re-locate the y axis to the x=0 point. This seems like a fairly basic operation, but I foun开发者_高级运维d no way to do that... Thanks for any help!
x <- runif(50, -10, 10)
y <- runif(50, -10, 10)
plot(x, y, yaxt="n") # don't plot y-axis, see ?par, section xaxt
axis(2, pos=0) # Draw y-axis at 0 line
But personally I think that you should use grid()
or Andrie solution.
Create some data
x <- runif(50, -10, 10)
y <- runif(50, -10, 10)
In base graphics, you can use the abline
function to draw lines on a plot. The trick is to draw a vertical line and horizontal line at the x=0
and y=0
positions:
plot(x, y)
abline(h=0)
abline(v=0)
An alternative way of achieving a similar result is to use the ggplot2
package:
library(ggplot2)
qplot(x, y) + geom_vline(xintercept=0) + geom_hline(yintercept=0)
精彩评论