Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Aug 31 14:36:47 CEST 2010

```On Tue, 2010-08-31 at 04:42 -0700, Stephen Liu wrote:
> Hi Greg,
>
>
> Performed following test:-
>
> > attach(women)
> > library(lattice)
> > x11(height=6, width=18)
> > par(mfrow=c(1,3))
> > plot(weight,height)
> >
> > attach(singer)
>
>     The following object(s) are masked from women :
>
>      height
>
> > plot(weight,height)
> Error in xy.coords(x, y, xlabel, ylabel, log) :
>   'x' and 'y' lengths differ
>
> Why no graph plotted?

Because you haven't understood what we've been trying to tell you!

What do you think you've plotted there? Where do height and weight come
from, to lead R to think that they have different lengths?

Think about it... R has even given you a warning (Hint, the masked
message)...

Got it yet...?

with(singer, length(height))
with(women, length(height))
names(singer)

So no singer\$weight, so R did:

plot(women\$weight, singer\$height)

and quite rightly tells us that

length(women\$weight) == length(singer\$height)

All because you attached data...

Try thinking about code rather than just running it.

> > attach(singer[1:15,])

Why? Why would you do this??? You've now plotted the first 15 heights of
object 'singer' against the weights of object 'women'. It makes no
sense. This is the points.

>     The following object(s) are masked from singer :
>
>      height voice.part
>
>
>     The following object(s) are masked from women :
>
>      height
>
> > plot(weight,height)
>
> >
> > with(women,plot(weight,height))
>
> Comparing 3 graphs, 1st and 3rd being the same.  On the 2rd graph the x-axis is
> women.

Yes.

But don't use attach() unless you really, really, really know what you
are doing, and even then probably don't do it.

G

>
>
> B.R.
> Stephen L
>
>
>
>
>
>
>
>
> ----- Original Message ----
> From: Greg Snow <Greg.Snow at imail.org>
> To: Stephen Liu <satimis at yahoo.com>; "gavin.simpson at ucl.ac.uk"
> <gavin.simpson at ucl.ac.uk>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Tue, August 31, 2010 2:10:51 AM
> Subject: RE: [R] About plot graphs
>
> Stephen,
>
> You may have missed an important point in my post (or you may have understood
> it, but not stating it hear could mislead future readers of the exchange.  Below
> you have:
>
> > > detach()
> > > attach(singer[1:15,])
> >
> >     The following object(s) are masked from women :
> >
> >      height
> >
> > > plot(weight,height)
> >
> > A graph weight(x) height(y) plotted.
> >
> >
>
> But you need to look really closely at that plot created, this is one of the
> most dangerous plots that R can produce.  It looks fine, there are no errors or
> warnings (well there was a warning earlier, but most people have  forgotten
> about it by the time they look at this plot).
>
> You say that it is a graph of weight(x) height(y), while that is technically
> true it should really be expanded:
>
> It is a plot of the weight from the dataset "women" on the x axis and the
> heights of the 1st 15 singers (singer dataset) on the y axis, there is no
> linkage between the 2 datasets (other than both having a height column), so this
> plot is completely meaningless (look at the plot compared to with(women,
> plot(weight,height)) ).  It is even worse than meaningless because it could
> encourage someone to make the exactly wrong conclusion about the relationship.
>
> My last example there was not showing how to work around a warning/error, but a
> dire caution about how using/misusing attach can lead to wrong decisions.
> Functions like with and within are much preferred for not leading to these types
> of problems.
>
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
> > -----Original Message-----
> > From: Stephen Liu [mailto:satimis at yahoo.com]
> > Sent: Sunday, August 29, 2010 6:55 AM
> > To: Greg Snow; gavin.simpson at ucl.ac.uk
> > Cc: r-help at r-project.org
> > Subject: Re: [R] About plot graphs
> >
> > Hi Greg,
> >
> >
> > > data(women)
> > > women
> >    height weight
> > 1      58    115
> > 2      59    117
> > 3      60    120
> > 4      61    123
> > 5      62    126
> > 6      63    129
> > 7      64    132
> > 8      65    135
> > 9      66    139
> > 10     67    142
> > 11     68    146
> > 12     69    150
> > 13     70    154
> > 14     71    159
> > 15     72    164
> > >
> >
> > > attach(women)
> > > library(lattice)
> >
> > > data(singer)
> > Warning message:
> >
> >
> > Continued;
> >
> > > attach(singer)
> >
> >     The following object(s) are masked from women :
> >
> >      height
> >
> > > plot(weight,height)
> > Error in xy.coords(x, y, xlabel, ylabel, log) :
> >   'x' and 'y' lengths differ
> >
> > No graph plotted.
> >
> >
> > > detach()
> > > attach(singer[1:15,])
> >
> >     The following object(s) are masked from women :
> >
> >      height
> >
> > > plot(weight,height)
> >
> > A graph weight(x) height(y) plotted.
> >
> >
> > B.R.
> > Stephen L
> >
> >
> >
> >
> >
> >
> > ----- Original Message ----
> > From: Greg Snow <Greg.Snow at imail.org>
> > To: Stephen Liu <satimis at yahoo.com>; "gavin.simpson at ucl.ac.uk"
> > <gavin.simpson at ucl.ac.uk>
> > Cc: "r-help at r-project.org" <r-help at r-project.org>
> > Sent: Sun, August 29, 2010 2:58:12 AM
> > Subject: RE: [R] About plot graphs
> >
> > Gavin gave some problems with relying attaching data, here is another
> > example,
> > somewhat artificial, but not unrealistic (I had similar to this happen
> > to me
> > before I learned better):
> >
> > attach(women)
> > # do some stuff
> > library(lattice)
> > attach(singer)
> > # do some more stuff
> >
> > # now we want to go back and look at the women data
> > plot(weight,height)
> >
> > #or even worse
> > detach()
> > attach(singer[1:15,])
> >
> > plot(weight,height)
> >
> > # what conclusions do we draw from the plot?
> >
> > detach()
> > detach()
> >
> >
> >
> >
> > --
> > Gregory (Greg) L. Snow Ph.D.
> > Statistical Data Center
> > Intermountain Healthcare
> > greg.snow at imail.org
> > 801.408.8111
> >
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > > project.org] On Behalf Of Stephen Liu
> > > Sent: Friday, August 27, 2010 11:14 PM
> > > To: gavin.simpson at ucl.ac.uk
> > > Cc: r-help at r-project.org
> > > Subject: Re: [R] About plot graphs
> > >
> > > Hi Gavin,
> > >
> > >
> > > Thanks for your advice and the examples explaining plotting settings.
> > >
> > > The steps on your examples work on my test.
> > >
> > >
> > > > 2) Don't attach() things, you are asking for trouble
> > >
> > > > If a function has a formula method (which plot does) then use it
> > like
> > > > this: plot(Draft_No. ~ Day_of_year, data = Test01)
> > >
> > > > If the function doesn't have a formula method, then wrap it in a
> > > > with()
> > > > call:
> > >
> > > > with(Test01, plot(Day_of_year, Draft_No.))
> > >
> > > > No need for attach.
> > >
> > >
> > > Noted and thanks.  What will be the problem caused by "attach()"?
> > >
> > >
> > > > dev.new(height = 6, width = 12)
> > > > layout(matrix(1:2, ncol = 2))
> > > > op <- par(pty = "s") ## this is the important bit
> > > > plot(runif(100), rnorm(100))
> > > > plot(runif(100), rnorm(100), col = "red")
> > > > par(op) ## now reset the pars
> > > > layout(1)
> > >
> > > What is the function of layout(1) ?  Tks
> > >
> > >
> > > B.R.
> > > satimis
> > >
> > >
> > >
> > >
> > > ----- Original Message ----
> > > From: Gavin Simpson <gavin.simpson at ucl.ac.uk>
> > > To: Stephen Liu <satimis at yahoo.com>
> > > Cc: "r-help at r-project.org" <r-help at r-project.org>
> > > Sent: Fri, August 27, 2010 5:38:40 PM
> > > Subject: Re: [R] About plot graphs
> > >
> > > On Fri, 2010-08-27 at 02:05 -0700, Stephen Liu wrote:
> > > > Hi Gavin,
> > > >
> > > > Thanks for your advice which works for me.
> > > >
> > > >
> > > > (rectangular window)
> > > > dev.new(height = 6, width = 12)
> > > > layout(matrix(1:2, nrow=1))
> > > > plot(Test01\$Day_of_year, Test01\$Draft_No.)
> > > > attach(Test01)
> > > > plot(Day_of_year,Draft_No.)
> > >
> > > 1) I can't reproduce this; where/what is Test01? But don;t bother
> > > sending, see my example below
> > > 2) Don't attach() things, you are asking for trouble
> > >
> > > If a function has a formula method (which plot does) then use it like
> > > this: plot(Draft_No. ~ Day_of_year, data = Test01)
> > >
> > > If the function doesn't have a formula method, then wrap it in a
> > with()
> > > call:
> > >
> > > with(Test01, plot(Day_of_year, Draft_No.))
> > >
> > > No need for attach.
> > >
> > > >
> > > > (rectangular window in vertical position)
> > > > dev.new(height = 12, width = 4)
> > > > layout(matrix(1:2, nrow=2))
> > > > plot(Test01\$Day_of_year, Test01\$Draft_No.)
> > > > plot(Day_of_year,Draft_No.)
> > > >
> > > > (height = 12, width = 6) can't work.  The graphs plotted are
> > > distorted off
> > > > square shape.  I must reduce "width = 4"
> > > >
> > > > Why?  TIA
> > >
> > > Because you don't appreciate that the dimensions of the device are
> > not
> > > the same as the dimensions of the plotting region *on* the device.
> > Most
> > > notably, the margins on the device are given by par("mar") for
> > example
> > > and are not square:
> > >
> > > > par("mar")
> > > [1] 5.1 4.1 4.1 2.1
> > >
> > > So more space is set aside on the bottom then anywhere else, and the
> > > margin on the right is quite small.
> > >
> > > You have already been provided with an answer that you dismissed
> > > because
> > > you didn't appear to appreciate what you were being told.
> > >
> > > Compare this:
> > >
> > > dev.new(height = 6, width = 12)
> > > layout(matrix(1:2, ncol = 2))
> > > plot(runif(100), rnorm(100))
> > > plot(runif(100), rnorm(100), col = "red")
> > > layout(1)
> > >
> > > with this:
> > >
> > > dev.new(height = 6, width = 12)
> > > layout(matrix(1:2, ncol = 2))
> > > op <- par(pty = "s") ## this is the important bit
> > > plot(runif(100), rnorm(100))
> > > plot(runif(100), rnorm(100), col = "red")
> > > par(op) ## now reset the pars
> > > layout(1)
> > >
> > > So now the regions are square, we have the asymmetric margins like
> > all
> > > R
> > > plots and we have drawn this on a device that has ~ appropriate
> > > dimensions.
> > >
> > > If you want to fiddle more with this, then you'll need to make the
> > > margins equal, but you don't want to do that really as you need more
> > > space in certain areas to accommodate axis labels and tick labels
> > etc.
> > >
> > > For the vertical one, this works for me, though note that because of
> > > the
> > > margins, pty = "s" is making the individual plotting regions smaller
> > to
> > > respect the square plotting region you asked for.
> > >
> > > dev.new(height = 12, width = 6)
> > > layout(matrix(1:2, ncol = 1))
> > > op <- par(pty = "s") ## this is the important bit
> > > plot(runif(100), rnorm(100))
> > > plot(runif(100), rnorm(100), col = "red")
> > > par(op) ## now reset the pars
> > > layout(1)
> > >
> > > This is because you have 5.1 plus 4.1 lines of margin in the vertical
> > > direction per plot (so 18.4 lines in total) versus 4.1 + 2.1 = 6.2
> > > lines
> > > of margin in the horizontal  direction. So to make the plots square,
> > > the
> > > horizontal direction is restricted. If we take a bit of space out of
> > > the
> > > top/bottom margins, things improve (note I reduce the height as it
> > > doesn't fit properly on my monitor):
> > >
> > > dev.new(height = 10, width = 5)
> > > layout(matrix(1:2, ncol = 1))
> > > op <- par(pty = "s", mar = c(4,4,3,2) + 0.1)
> > > plot(runif(100), rnorm(100))
> > > plot(runif(100), rnorm(100), col = "red")
> > > par(op) ## now reset the pars
> > > layout(1)
> > >
> > > So as we reduce the vertical space required for margins, the square
> > > panels start to occupy more and more of the total device.
> > >
> > > Finally, notice how I provided examples that *you*, *me* and *anyone*
> > > else on the list can use to test behaviour/point out problems. Ths is
> > > what we call a reproducible example. If you want help without going
> > > round the houses (lots of dead ends), specifying an example like I
> > did
> > > (your problem is not with *your* data but with using the R functions,
> > > so
> > > who cares what the data are?) above allows us very quickly to home in
> > > on
> > > the problem you have.
> > >
> > > > Looked at ?dev.new
> > > > can't resolve.
> > > >
> > > > Whether use another command such as;
> > > >  dev.cur()
> > > >      dev.list()
> > > >      dev.next(which = dev.cur())
> > > >      dev.prev(which = dev.cur())
> > > >      dev.off(which = dev.cur())
> > > >      dev.set(which = dev.next())
> > > >      graphics.off()
> > > > ?
> > >
> > > If you had read ?dev.new (and understood it), you would know that
> > those
> > > commands you list can't possibly help.
> > >
> > > HTH
> > >
> > > G
> > >
> > > >
> > > >
> > > > B.R.
> > > > Stephen L
> > > >
> > > >
> > > >
> > > >
> > > > ----- Original Message ----
> > > > From: Gavin Simpson <gavin.simpson at ucl.ac.uk>
> > > > To: Stephen Liu <satimis at yahoo.com>
> > > > Cc: "r-help at r-project.org" <r-help at r-project.org>
> > > > Sent: Fri, August 27, 2010 4:21:13 PM
> > > > Subject: Re: [R] About plot graphs
> > > >
> > > > On Thu, 2010-08-26 at 21:01 -0700, Stephen Liu wrote:
> > > > > Hi Greg,
> > > > <snip />
> > > > > > windows(width=12, height=6)
> > > > > Error: could not find function "windows"
> > > >
> > > > So you aren't on Windows then... Hence why the posting guide asks
> > for
> > > > sessionInfo() details; sometimes it matters.
> > > >
> > > > Anyway, a OS independent way of doing this is to use dev.new() and
> > > pass
> > > > along the arguments you would have provided to the device via e.g.
> > > > windows():
> > > >
> > > > dev.new(height = 6, width = 12)
> > > >
> > > > HTH
> > > >
> > > > G
> > > >
> > > > >
> > > > > > ?windows
> > > > > No documentation for 'windows' in specified packages and
> > libraries:
> > > > > you could try '??windows'
> > > > >
> > > > >
> > > > > > window(width=12, height=6)
> > > > > Error in hasTsp(x) :
> > > > >   element 1 is empty;
> > > > >    the part of the args list of 'attr' being evaluated was:
> > > > >    (x, "tsp")
> > > > >
> > > > >
> > > > > > ?window
> > > > > window {stats}
> > > > > http://stat.ethz.ch/R-manual/R-
> > devel/library/stats/html/window.html
> > > > >
> > > > > window                  package:stats                  R
> > > Documentation
> > > > >
> > > > > Time Windows
> > > > >
> > > > > Description:
> > > > >
> > > > >      ‘window’ is a generic function which extracts the subset of
> > > the
> > > > >      object ‘x’ observed between the times ‘start’ and ‘end’. If
> > a
> > > > >      frequency is specified, the series is then re-sampled at the
> > > new
> > > > >      frequency.
> > > > >
> > > > >
> > > > > > window(layout(matrix(1:2, nrow=1), width=12, height=6))
> > > > > [1] 2
> > > > > attr(,"tsp")
> > > > > [1] 1 1 1
> > > > >
> > > > >
> > > > > Still pop up a square window
> > > > >
> > > > >
> > > > > B.R
> > > > > Stephen L
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ----- Original Message ----
> > > > > From: Greg Snow <Greg.Snow at imail.org>
> > > > > To: Stephen Liu <satimis at yahoo.com>; "r-help at r-project.org"
> > > > > <r-help at r-project.org>
> > > > > Sent: Fri, August 27, 2010 10:51:21 AM
> > > > > Subject: RE: [R] About plot graphs
> > > > >
> > > > > When you run any graphics command (layout in this case) and there
> > > is not a
> > > > > current graphics device (more technically only the null device)
> > > then a
> > > >default
> > > >
> > > >
> > > > > graphics device is opened, that is what you are seeing.  What you
> > > need to do
> > >
> > > > > instead is open the device yourself before calling layout.  Which
> > > device that
> > > >
> > > > >is
> > > > >
> > > > > depends greatly on information that the posting guide strongly
> > > suggests that
> > >
> > > > >you
> > > > >
> > > > > provide (another hint).
> > > > >
> > > > > One possibility is:
> > > > >
> > > > > > windows(width=12, height=6)
> > > > >
> > > > > Followed by layout and the plotting commands.  But whether that
> > > will work on
> > >
> > > > > your machine or not is still a bit of a mystery.
> > > > >
> > > > > --
> > > > > Gregory (Greg) L. Snow Ph.D.
> > > > > Statistical Data Center
> > > > > Intermountain Healthcare
> > > > > greg.snow at imail.org
> > > > > 801.408.8111
> > > > >
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > > > > > project.org] On Behalf Of Stephen Liu
> > > > > > Sent: Thursday, August 26, 2010 8:02 PM
> > > > > > To: r-help at r-project.org
> > > > > > Subject: Re: [R] About plot graphs
> > > > > >
> > > > > > Hi Greg,
> > > > > >
> > > > > >
> > > > > > I'm not prepared altering the shape of the graphs to be
> > plotted.
> > > What
> > > > > > I'm
> > > > > > trying to do is to pop up a rectangle layout window with
> > > following
> > > > > > command.
> > > > > >
> > > > > > The command;
> > > > > > layout(matrix(1:2, nrow=1))
> > > > > >
> > > > > > pop up a square window.  What I need is a rectangular window
> > for
> > > the
> > > > > > graphs to
> > > > > > be plotted.  Otherwise the graphs are squeezed changing shape.
> > > > > >
> > > > > > I looked at ?layout but can't resolve how to make it.  Can you
> > > help?
> > > > > > TIA
> > > > > >
> > > > > > B.R.
> > > > > > Stephen L
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > ----- Original Message ----
> > > > > > From: Greg Snow <Greg.Snow at imail.org>
> > > > > > To: Stephen Liu <satimis at yahoo.com>; "r-help at r-project.org"
> > > > > > <r-help at r-project.org>
> > > > > > Sent: Fri, August 27, 2010 9:00:01 AM
> > > > > > Subject: RE: [R] About plot graphs
> > > > > >
> > > > > > There is a graphical parameter that controls whether a plot is
> > > square
> > > > > > or takes
> > > > > > up the maximum amount of room (rectangle), see ?par and look at
> > > the
> > > > > > entry for
> > > > > > pty.
> > > > > >
> > > > > >
> > > > > > It is possible that you set pty='s' or it may be that the plot
> > > method
> > > > > > sets it,
> > > > > > without us knowing what type of object Date and Test01\$Date are
> > > we
> > > > > > don't know
> > > > > > which method is creating your plot and cannot be much more help
> > > (that
> > > > > > is meant
> > > > > > as a subtle hint to provide the information requested in the
> > > footer of
> > > > > > every
> > > > > > post and the posting guide).
> > > > > >
> > > > > > Some methods may set pty='s' as default but have an option to
> > > change
> > > > > > it.
> > > > > >
> > > > > > --
> > > > > > Gregory (Greg) L. Snow Ph.D.
> > > > > > Statistical Data Center
> > > > > > Intermountain Healthcare
> > > > > > greg.snow at imail.org
> > > > > > 801.408.8111
> > > > > >
> > > > > >
> > > > > > > -----Original Message-----
> > > > > > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > > > > > > project.org] On Behalf Of Stephen Liu
> > > > > > > Sent: Thursday, August 26, 2010 8:45 AM
> > > > > > > To: r-help at r-project.org
> > > > > > > Subject: [R] About plot graphs
> > > > > > >
> > > > > > > Hi folks,
> > > > > > >
> > > > > > > Following command prints 2 graphs side-by-side:-
> > > > > > > layout(matrix(1:2, nrow=1))
> > > > > > > plot(Date,Input_No.)
> > > > > > > plot(Test01\$Date, Test01\$Input_No.)
> > > > > > >
> > > > > > > However each is a square graph I need a rectangular layout.
> > > Pls
> > > > > > advise
> > > > > > > how to
> > > > > > > make it.  TIA
> > > > > > >
> > > > > > > B.R.
> > > > > > > satimis
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > ______________________________________________
> > > > > > > R-help at r-project.org mailing list
> > > > > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > > > PLEASE do read the posting guide http://www.R-
> > > project.org/posting-
> > > > > > > guide.html
> > > > > > > and provide commented, minimal, self-contained, reproducible
> > > code.
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > ______________________________________________
> > > > > > R-help at r-project.org mailing list
> > > > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > > PLEASE do read the posting guide http://www.R-
> > > project.org/posting-
> > > > > > guide.html
> > > > > > and provide commented, minimal, self-contained, reproducible
> > > code.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ______________________________________________
> > > > > R-help at r-project.org mailing list
> > > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > PLEASE do read the posting guide http://www.R-
> > project.org/posting-
> > > guide.html
> > > > > and provide commented, minimal, self-contained, reproducible
> > code.
> > > >
> > >
> > > --
> > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > > 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
> > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > >
> > >
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```