Dear R-helpers,
I have a number of dataframes that looks something like this:
mydfr <- data.frame(treatment=c(rep("A",3),rep("B",3)),
Xmeas=1:6,
Ymeas=c(2,4,3,3,5,6))
# except with many more variables, which I plot all the time colored
by treatment to quickly check things:
with(mydfr, plot(Xmeas, Ymeas, pch=19,
col=c("blue","red")[treatment]))
# I find it annoying to keep typing in all the bits after Ymeas (yes I
use a text editor but that's not the point...)
# 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
I don't understand why the 'with' statement does not allow the
'myplot' function to find the 'treatment' variable (after all,
it
finds the other variables just fine).
Can someone explain why this is, and how to fix this?
thanks for your help
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
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Sep-14 08:08 UTC
[R] How to set default plotting colors by treatment?
> > # 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}}
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Sep-14 11:53 UTC
[R] How to set default plotting colors by treatment?
>> 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...In that case I'd try something like: myplot <- function(..., tmnt) { plot(..., pch=19, col=c("blue","red")[tmnt] ) } with(mydfr, myplot(Xmeas, Ymeas, tmnt=treatment)) That seems to work... - basically you just need to pass treatment in the function call... ******************************************************************************************************************** This message may contain confidential information. If yo...{{dropped:21}}