18

Suppose I want to plot x^2. I can use curve() as follows.

curve(x^2, -5, 5)

enter image description here However, I would like the axes to go through (0, 0). I could do something as follows:

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0)
axis(2, pos=0)
abline(h=0)
abline(v=0)

And I end up getting something like below, which looks OK. But the only gripe I have is that this way of plotting axes makes the actual axes - for example the segment between -4 and 4 of the x-axis - thicker than the segments to the right side and the left side. The same goes with the y axis. I wonder if there is a better way of plotting the axes. Thank you!

enter image description here

Thomas
  • 43,637
  • 12
  • 109
  • 140
Alex
  • 4,030
  • 8
  • 40
  • 62
  • Why do you want to make your plots look like the ones Excel produces? – Gavin Simpson Jan 26 '13 at 19:13
  • @GavinSimpson: Ha! I actually didn't know Excel made plots that looked this way. Rarely use Excel for plotting purposes. For my data, the (0, 0) is important, so I thought letting the axes cross this point would make more sense. – Alex Jan 26 '13 at 19:58
  • The answer you checked is great; just thought I'd throw this option in: do the default plot, and then add identifier lines wherever you want them, e.g. `lines(c(0,0), c(-1,10),lty=4)` for a y-axis at x=0 – Carl Witthoft Jan 26 '13 at 20:46
  • 1
    @Alex IMHO I think the default R output is much better (no axis in the middle of the important data, tick labels not obscured by data or other axes, ...). If (0,0) is important, I would just add a lightgrey line using `abline(v = 0)` and `abline(h = 0)` calls, preferably in the background. – Gavin Simpson Jan 26 '13 at 21:02
  • 1
    @gavin -- sounds like what I wrote :-) – Carl Witthoft Jan 26 '13 at 23:16

2 Answers2

16

By default, axis() computes automatically the tick marks position, but you can define them manually with the at argument. So a workaround could be something like :

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=-5:5)
axis(2, pos=0)

Which gives :

enter image description here

The problem is that you have to manually determine the position of each tick mark. A slightly better solution would be to compute them with the axTicks function (the one used by default) but calling this one with a custom axp argument which allows you to specify respectively the minimum, maximum and number of intervals for the ticks in the axis :

curve(x^2, -5, 5, axes=FALSE)
axis(1, pos=0, at=axTicks(1,axp=c(-10,10,10)))
axis(2, pos=0)

Which gives :

enter image description here

juba
  • 47,631
  • 14
  • 113
  • 118
3

The arguments yaxs and xaxs control spacing around plots. Set to "i" to omit this:

curve(x^2, -5, 5, yaxs = "i")

See also: https://stackoverflow.com/a/12300673/567015

Community
  • 1
  • 1
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • This option trims at both ends, not just at (0,0) with bad side-effects for some plots, e.g. plot with a lot of data points spread flat near the top, as with a bell-shaped curve. Also, for some reasons, the axis will overwrite the dots/lines, making data near the ends hard to see. – PatrickT Mar 18 '13 at 08:06