[R] r-plot

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed May 27 13:44:27 CEST 2009


On Wed, 2009-05-27 at 02:52 -0700, durden10 wrote:
> Dear R-community
> 
> I have a grueling problem which appears to be impossible to solve:
> I want to make a simple plot, here is my code: http://gist.github.com/118550
> Unfortunately, the annotation of both the x- and y-axis are not correct, as
> you can see in the following picture: 
> http://www.nabble.com/file/p23739356/plot.png 
> I am not an expert of R, so maybe someone can point me to the solution of
> this problem, i.e. both of the axes should start and end at the min / max
> values of the two vectors.

But you asked it to do that, explicitly:

par(tcl=0.35,xaxs="r")

xaxs = "r" is the default and if you read ?par it will tell you that
this extends the range of the plot by 4%. Pretty labels are then found
within this range.

Try xaxs = "i" and yaxs = "i" in your call instead.

Then you do this:

axis(2, tcl=0.35,at=0:11)

But 11 is outside the range of the plotted data (+4%) so this tick isn't
drawn. The plot() call sets up the region - a subsequent call to axis()
won't change the axis limits. If you want it to extend up to 11, then
add:

ylim = c(0,11) in your call to plot.

Note also that either the tcl in the first par() call or the ones in the
two axis calls is redundant. Use one or the other.

Here is a simplified example:

set.seed(123)
y <- 0:11 + rnorm(12)
x <- runif(12)
## if you want the 4% padding, then xaxs = "r" etc
op <- par(xaxs = "i", yaxs = "i", tcl = 0.35)
plot(x, y, ylim = c(0,11), axes = FALSE)
axis(2, at = 0:11)
axis(1)
box()
par(op)

Finally, as you have your data in a DF, you could make use of this
instead of relying on getting the ordering correct, and also simplify
your lm call:

plot(Calgary ~ Win, data = data_corr, ....)

and

abline(lm(Calgary ~ Win, data = data_corr, ....))

would be a better way to make use of the formula interface, and be
explicit in the plot about which variable is on the x and which is on
the y axis.

HTH

G

> 
> Thanks in advance!!
> 
> Best,
> Durden
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090527/bc7b0c0a/attachment-0002.bin>


More information about the R-help mailing list