[R] About plot graphs
David Winsemius
dwinsemius at comcast.net
Sat Aug 28 19:52:50 CEST 2010
On Aug 28, 2010, at 9:15 AM, Stephen Liu wrote:
> Hi Gavin,
>
> Lot of thank for your detail explanation.
>
> Just looked at;
> ?with
> ?within
>
> Both "Evaluate an Expression in a Data Environment"
>
> Usage: Both;
> with(data, expr, ...)
> within(data, expr, ...)
>
> Details:
> .....
> ‘within’ is similar, except that it examines the environment after
> the evaluation of ‘expr’ and makes the corresponding modifications
> to ‘data’ (this may fail in the data frame case if objects are
> created which cannot be stored in a data frame), and returns it.
> ‘within’ can be used as an alternative to ‘transform’.
>
> What does it mean "..... examines the environment after the
> evaluation of 'expr'
I take it to mean that the results of expr (applied to "data" and
anything else used as arguments) are then used to update "data", but
only if possible, i.e., if those results are congruent with the
structure of "data". I read that phrase as covering the "if possible"
assessment process.
> ....."
>
> Tks
>
> B.R.
> Stephen
>
>
> 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: Sat, August 28, 2010 3:28:34 PM
> Subject: Re: [R] About plot graphs
>
> On Fri, 2010-08-27 at 22:14 -0700, Stephen Liu wrote:
>> 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()"?
>
> If you change the underlying data, this is not reflected in the
> attached
> copy, because it is just that, a "copy"[1] created at the point at
> which
> you attached the object. E.g.
>
> ## Some data, which we attach
> dat <- data.frame(A = 1:10, B = letters[1:10])
> attach(dat)
> ## Look at A
> A
> ## Change or dat object by altering the A component
> dat <- within(dat, A <- LETTERS[1:10])
> ## Look at A
> A
> ## Look at what A really is
> with(dat, A)
>
> Using with() and within() etc have two key advantages over attach: i)
> only one version of the data/object exists, ii) the intention of code
> using:
>
> with(dat, "do something with A")
>
> is much more clear than
>
> "do something with A"
>
> A could be anything, anywhere. More info is on the ?attach help page.
>
> [1] ?attach contains the details of what I mean by "copy"
>
>>
>>> 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
>
> The opposite of
>
> layout(matrix(1:4, ncol = 2))
>
> for example. It ( layout(1) ) says create a layout with a single
> plotting region. So we use it to reset the current device back to
> normal. I find it is good working practice to tidy up after doing
> plots
> like this. In the code above, we change both the layout() and the
> plotting parameters (via par() ), and the last two lines of code in
> that
> example reset these changes.
>
> G
>
>>
>> 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,
>>>>>
>>>>> Thanks for your advice.
>>>>>
>>>>> 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
>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>>
>>
>
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list