[R] How to set default plotting colors by treatment?

Paul Hiemstra p.hiemstra at geo.uu.nl
Mon Sep 14 14:00:35 CEST 2009


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

cheers and hope this helps,
Paul

Remko Duursma wrote:
> The example is reproducible! Did you see the first post?
>
> remko
>
>
>
> -------------------------------------------------
> Remko Duursma
> Post-Doctoral Fellow
>
> Centre for Plants and the Environment
> University of Western Sydney
> Hawkesbury Campus
> Richmond NSW 2753
>
> Dept of Biological Science
> Macquarie University
> North Ryde NSW 2109
> Australia
>
> Mobile: +61 (0)422 096908
> www.remkoduursma.com
>
>
>
> On Mon, Sep 14, 2009 at 9:19 PM, Paul Hiemstra <p.hiemstra at geo.uu.nl> wrote:
>   
>> Remko Duursma wrote:
>>     
>>>> col=c("blue","red")mydfr$[treatment]
>>>>
>>>>         
>>> Yes, but I would like to use the function for lots of other dataframes
>>> as well, so embedding 'mydfr' in the function is not the ideal
>>> solution...
>>>
>>>       
>> The problem is that the info in 'treatment' is non-constant, and you need to
>> either pass on the info into the scope of the function, or you need to
>> calculate the values in 'treatment' inside the function. Could you provide
>> us with a reproducible example (as suggested in the posting guide), that
>> would make it much easier for us to answer you question
>>
>> veel succes!
>> Paul
>>     
>>> remko
>>>
>>> -------------------------------------------------
>>> Remko Duursma
>>> Post-Doctoral Fellow
>>>
>>> Centre for Plants and the Environment
>>> University of Western Sydney
>>> Hawkesbury Campus
>>> Richmond NSW 2753
>>>
>>> Dept of Biological Science
>>> Macquarie University
>>> North Ryde NSW 2109
>>> Australia
>>>
>>> Mobile: +61 (0)422 096908
>>> www.remkoduursma.com
>>>
>>>
>>>
>>> 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
>>>>>
>>>>>
>>>>>           
>>>> basically that is something like calling:
>>>>
>>>> myplot( mydfr$Xmeas, mydfr$Ymeas )
>>>>
>>>> So plot doesn't know that treatment is within mydfr...
>>>>
>>>> changing your function to:
>>>>
>>>> myplot <- function(...) {
>>>> plot(...,
>>>>      pch=19,
>>>>      col=c("blue","red")mydfr$[treatment]
>>>>     )
>>>> }
>>>>
>>>> should work?
>>>>
>>>>
>>>> ********************************************************************************************************************
>>>>
>>>> This message may contain confidential information. If yo...{{dropped:21}}
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>>       
>> --
>> Drs. Paul Hiemstra
>> Department of Physical Geography
>> Faculty of Geosciences
>> University of Utrecht
>> Heidelberglaan 2
>> P.O. Box 80.115
>> 3508 TC Utrecht
>> Phone:  +3130 274 3113 Mon-Tue
>> Phone:  +3130 253 5773 Wed-Fri
>> http://intamap.geo.uu.nl/~paul
>>
>>
>>     


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul




More information about the R-help mailing list