[R] How to set default plotting colors by treatment?
David Winsemius
dwinsemius at comcast.net
Mon Sep 14 14:36:49 CEST 2009
On Sep 14, 2009, at 8:00 AM, Paul Hiemstra wrote:
> I offer my sincere apologies for not reading the e-mail carefully,
> your example is indeed reproducible. When you stop using the 'with'
> function, this is I think what you would like:
>
> myplot2 = function(formula, data, ...) {
> plot(formula, data = data, ..., pch = 19, col = c("blue","red")
> [data$treatment])
> }
> myplot2(Ymeas~Xmeas, mydfr)
>
> A possible problem occurs when you want to redefine 'pch' or 'col',
> e.g.:
>
> myplot2(Ymeas~Xmeas, mydfr, pch = 20)
> Error in localWindow(xlim, ylim, log, asp, ...) :
> formal argument "pch" matched by multiple actual arguments
Generally one puts the defaults to arguments in the invocation section
of the function
myplot2 = function(formula, data, pch = 19,...) {
plot(formula, data = data, ..., col = c("blue","red"))
}
myplot2(Ymeas~Xmeas, mydfr, pch = 20) # no error
If you wanted to change the colors you could also use a construction
like:
..., firstcol="red", seccol="blue",
and then in the body of the function:
..., col=c(firstcol, seccol)[data$treatment]
snip
>>>> On Mon, Sep 14, 2009 at 6:08 PM, Polwart Calum (County Durham and
>>>> Darlington NHS Foundation Trust) <calum.polwart at nhs.net> wrote:
>>>>
>>>>
>>>>>> # I tried defining a function like this
>>>>>> myplot <- function(...)plot(..., pch=19, col=c("blue","red")
>>>>>> [treatment])
>>>>>>
>>>>>> # So i can call it like this:
>>>>>> with(mydfr, myplot(Xmeas, Ymeas))
>>>>>>
>>>>>> # but:
>>>>>> Error in plot.xy(xy, type, ...) : object 'treatment' not found
>>>>>>
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list