[R] setting x-y axis at origin

Marc Schwartz MSchwartz at medanalytics.com
Mon Mar 15 19:35:26 CET 2004


On Mon, 2004-03-15 at 08:45, christopher ciotti wrote:
> Hello -
> 
> I'm just getting into 'R' and am having trouble setting up the x-y axis 
> to share (0,0).  In the example posted here: 
> http://geocities.com/chris_ciotti/Images/part1.pdf, each axis has a 0 
> which I do not want. 
> 
> Any help on getting a graph starting at (0,0) would be greatly 
> appreciated. 


What you are seeing is the default behavior for axis ranges, which are
extended by 4% under default circumstances. This is described in the
help for 'par' under 'xaxs' when using the default axis style "r". See
?par for additional information.

So for example, if you use:

plot(c(0, 100), c(0,140))

You will see a generic scatter plot that has roughly the same axis
ranges as your plot.

You have a simple approach to alter this behavior by changing the axis
styles to "i".

plot(c(0, 100), c(0,140), xaxs = "i", yaxs = "i")

If your x and y values do not actually contain (0,0), you can explicitly
define the axis ranges by using 'xlim' and 'ylim' respectively:

plot(c(1, 100), c(1,140), xaxs = "i", yaxs = "i")

Note the above origin is at (1, 1). Use the following instead:

plot(c(1, 100), c(1,140), xaxs = "i", yaxs = "i", 
     xlim = c(0, 100), ylim = c(0, 140))

You can also adjust the xlim and ylim values as you require based upon
you actual data (ie. xlim = c(0, max(x)) )

You might want to review ?plot.default and ?axis for additional
information on some of the arguments available for fine tuning plots and
axes along with the help for par.

HTH,

Marc Schwartz




More information about the R-help mailing list