The position of the axis in the scattering plane R

I am trying to create a simple scatter chart in R where the x-axis range is -10: 10, and move the y-axis to x = 0. This seems like a pretty basic operation, but I did not find a way to do this ... Thanks for any help!

+5
source share
2 answers
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

x-axis on 0 line

But personally, I think you should use grid()or decision Andrie .

+7
source

Create some data

x <- runif(50, -10, 10)
y <- runif(50, -10, 10)

In the base chart, you can use the function ablineto draw lines on the chart. The trick is to draw a vertical line and a horizontal line in positions x=0and y=0:

plot(x, y)
abline(h=0)
abline(v=0)

enter image description here

ggplot2:

library(ggplot2)
qplot(x, y) + geom_vline(xintercept=0) + geom_hline(yintercept=0)

enter image description here

+3

All Articles