Re: [R] Groups and bwplot
You may want to consult a recent post by Felix (https://stat.ethz.ch/pipermail/r-help/2011-August/286707.html) on how to pass group parameter. Weidong Gu On Sat, Aug 20, 2011 at 6:59 AM, Sébastien Bihorel wrote: > Dear R-users, > > A while ago, Deepayan Sarkar suggested some code that uses the group > argument in bwplot to create some 'side-by-side' boxplots > (https://stat.ethz.ch/pipermail/r-help/2010-February/230065.html). The > example he gave was relatively specific and I wanted to generalize his > approach into a function. Unfortunately, I seem to have some issues > passing the correct arguments to the panel function, and would greatly > appreciate any suggestions to solve these issues: > > require(lattice) > > mybwplot <- function(x,y,data,groups){ > > if (missing(groups)||is.null(groups)) { > groups <- NULL > ngroups <- 1 > } else { > data[[groups]] <- as.factor(data[[groups]]) > ngroups <- nlevels(data[[groups]]) > } > mywidth <- 1/(ngroups+1) > > mypanel <- function(x,y,groups,...){ > if (missing(groups)||is.null(groups)) { > panel.bwplot(x,y,...) > } else { > panel.superpose(x,y,...) > } > } > > mypanel.groups <- function(x,y,groups,ngroups,...){ > if (missing(groups)||is.null(groups)){ > NULL > } else { > function(x, y, ..., group.number) { > panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...)} > } > } > > bwplot(formula(paste(y,x,sep=' ~ ')), > data = data, > groups = 'Plant', > ngroups=ngroups, > pch = "|", > box.width = mywidth, > panel = mypanel, > panel.groups = mypanel.groups) > > } > > myCO2 <- CO2 > myCO2$year <- 2011 > summary(myCO2) > > mybwplot('Type','uptake',myCO2) # works > > mybwplot('Type','uptake',myCO2,'Treatment') # Error using packet 1, > 'x' is missing > > mybwplot('Type','uptake',myCO2,'year') # Error using packet 1, 'x' is missing > > # Deepayan Sarkar suggested code (adapted to myC02) > # bwplot(uptake ~ Plant, data = myCO2, groups = Treatment, > # pch = "|", box.width = 1/3, > # panel = panel.superpose, > # panel.groups = function(x, y, ..., group.number) { > # panel.bwplot(x + (group.number-1.5)/3, y, ...) > # }) > > __ > R-help@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@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.
Re: [R] Groups and bwplot
Thanks for your input and this link. I realize that there was a typo in my example code that impacted the group argument... That's king of stupid. However, even with the implementation of Felix's syntax, the "Error using packet 1, 'x' is missing" error message is still displayed, even if the call appears correct. So I believe that group argument in not the issue but rather my panel functions. (Plus, Felix's notation creates side-issues such as the calculation of ngroups, which I have hard-coded in the following modified example). require(lattice) mybwplot <- function(x,data,groups){ if (missing(groups)) { ngroups <- 1 } else { ngroups <- 2 } mywidth <- 1/(ngroups+1) mypanel <- function(x,y,groups,...){ if (missing(groups)||is.null(groups)) { panel.bwplot(x,y,...) } else { panel.superpose(x,y,...) } } mypanel.groups <- function(x,y,groups,ngroups,group.number,...){ if (missing(groups)||is.null(groups)){ NULL } else { panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...) } } ccall <- quote(bwplot(x, data = data, ngroups=ngroups, pch = "|", box.width = mywidth, panel = mypanel, panel.groups = mypanel.groups)) ccall$groups <- substitute(groups) str(ccall) eval(ccall) } myCO2 <- CO2 myCO2$year <- 2011 mybwplot(uptake~Type,myCO2) # works mybwplot(uptake~Type,myCO2,groups=Treatment) # Error using packet 1, 'x' is missing #mybwplot(uptake~Type,myCO2,groups=year) # Error using packet 1, 'x' is missing # Deepayan Sarkar suggested code (adapted to myC02) # bwplot(uptake ~ Type, data = myCO2, groups = Treatment, #pch = "|", box.width = 1/3, #panel = panel.superpose, #panel.groups = function(x, y, ..., group.number) { #panel.bwplot(x + (group.number-1.5)/3, y, ...) #}) On Sat, Aug 20, 2011 at 11:38 AM, Weidong Gu wrote: > You may want to consult a recent post by Felix > (https://stat.ethz.ch/pipermail/r-help/2011-August/286707.html) on how > to pass group parameter. > > Weidong Gu > > On Sat, Aug 20, 2011 at 6:59 AM, Sébastien Bihorel wrote: >> Dear R-users, >> >> A while ago, Deepayan Sarkar suggested some code that uses the group >> argument in bwplot to create some 'side-by-side' boxplots >> (https://stat.ethz.ch/pipermail/r-help/2010-February/230065.html). The >> example he gave was relatively specific and I wanted to generalize his >> approach into a function. Unfortunately, I seem to have some issues >> passing the correct arguments to the panel function, and would greatly >> appreciate any suggestions to solve these issues: >> >> require(lattice) >> >> mybwplot <- function(x,y,data,groups){ >> >> if (missing(groups)||is.null(groups)) { >> groups <- NULL >> ngroups <- 1 >> } else { >> data[[groups]] <- as.factor(data[[groups]]) >> ngroups <- nlevels(data[[groups]]) >> } >> mywidth <- 1/(ngroups+1) >> >> mypanel <- function(x,y,groups,...){ >> if (missing(groups)||is.null(groups)) { >> panel.bwplot(x,y,...) >> } else { >> panel.superpose(x,y,...) >> } >> } >> >> mypanel.groups <- function(x,y,groups,ngroups,...){ >> if (missing(groups)||is.null(groups)){ >> NULL >> } else { >> function(x, y, ..., group.number) { >> panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...)} >> } >> } >> >> bwplot(formula(paste(y,x,sep=' ~ ')), >> data = data, >> groups = 'Plant', >> ngroups=ngroups, >> pch = "|", >> box.width = mywidth, >> panel = mypanel, >> panel.groups = mypanel.groups) >> >> } >> >> myCO2 <- CO2 >> myCO2$year <- 2011 >> summary(myCO2) >> >> mybwplot('Type','uptake',myCO2) # works >> >> mybwplot('Type','uptake',myCO2,'Treatment') # Error using packet 1, >> 'x' is missing >> >> mybwplot('Type','uptake',myCO2,'year') # Error using packet 1, 'x' is missing >> >> # Deepayan Sarkar suggested code (adapted to myC02) >> # bwplot(uptake ~ Plant, data = myCO2, groups = Treatment, >> # pch = "|", box.width = 1/3, >> # panel = panel.superpose, >> # panel.groups = function(x, y, ..., group.number) { >> # panel.bwplot(x + (group.number-1.5)/3, y, ...) >> # }) >> >> __ >> R-help@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@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.
Re: [R] Groups and bwplot
I got it to works. The problem was mainly how data were passed to my panel and panel.groups function. For reference, here is what I ended with: require(lattice) mybwplot <- function(x,y,data,group){ if (missing(group)||is.null(group)) { group <- 'NULL' ngroups <- 1 } else { data[[group]] <- as.factor(data[[group]]) ngroups <- nlevels(data[[group]]) } mywidth <- 1/(ngroups+1) mypanel <- function(x,y,groups,...){ if (missing(groups) || is.null(groups) || ngroups==1) { panel.bwplot(x=x,y=y,...) } else { panel.superpose(x=x,y=y,groups=groups,...) } } mypanel.groups <- function(x,y,ngroups,group.number,...){ if (ngroups==1) { NULL } else { panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...) } } if (!is.null(group)) group <- data[[group]] bwplot(formula(paste(y,x,sep='~')), data = data, ngroups = ngroups, groups = group, pch = "|", box.width = mywidth, panel = mypanel, panel.groups = mypanel.groups) } myCO2 <- CO2 myCO2$year <- 2011 mybwplot('Type','uptake',myCO2) # works mybwplot('Type','uptake',myCO2,'Treatment') # works mybwplot('Type','uptake',myCO2,'year') # works On Sat, Aug 20, 2011 at 1:35 PM, Sébastien Bihorel wrote: > Thanks for your input and this link. I realize that there was a typo > in my example code that impacted the group argument... That's king of > stupid. > > However, even with the implementation of Felix's syntax, the "Error > using packet 1, 'x' is missing" error message is still displayed, even > if the call appears correct. So I believe that group argument in not > the issue but rather my panel functions. (Plus, Felix's notation > creates side-issues such as the calculation of ngroups, which I have > hard-coded in the following modified example). > > require(lattice) > > mybwplot <- function(x,data,groups){ > > if (missing(groups)) { > ngroups <- 1 > } else { > ngroups <- 2 > } > > mywidth <- 1/(ngroups+1) > > mypanel <- function(x,y,groups,...){ > if (missing(groups)||is.null(groups)) { > panel.bwplot(x,y,...) > } else { > panel.superpose(x,y,...) > } > } > > mypanel.groups <- function(x,y,groups,ngroups,group.number,...){ > if (missing(groups)||is.null(groups)){ > NULL > } else { > panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...) > } > } > > ccall <- quote(bwplot(x, > data = data, > ngroups=ngroups, > pch = "|", > box.width = mywidth, > panel = mypanel, > panel.groups = mypanel.groups)) > ccall$groups <- substitute(groups) > str(ccall) > eval(ccall) > > } > > myCO2 <- CO2 > myCO2$year <- 2011 > > mybwplot(uptake~Type,myCO2) # works > > mybwplot(uptake~Type,myCO2,groups=Treatment) # Error using packet 1, > 'x' is missing > > #mybwplot(uptake~Type,myCO2,groups=year) # Error using packet 1, 'x' is > missing > > # Deepayan Sarkar suggested code (adapted to myC02) > # bwplot(uptake ~ Type, data = myCO2, groups = Treatment, > # pch = "|", box.width = 1/3, > # panel = panel.superpose, > # panel.groups = function(x, y, ..., group.number) { > # panel.bwplot(x + (group.number-1.5)/3, y, ...) > # }) > > On Sat, Aug 20, 2011 at 11:38 AM, Weidong Gu wrote: >> You may want to consult a recent post by Felix >> (https://stat.ethz.ch/pipermail/r-help/2011-August/286707.html) on how >> to pass group parameter. >> >> Weidong Gu >> >> On Sat, Aug 20, 2011 at 6:59 AM, Sébastien Bihorel wrote: >>> Dear R-users, >>> >>> A while ago, Deepayan Sarkar suggested some code that uses the group >>> argument in bwplot to create some 'side-by-side' boxplots >>> (https://stat.ethz.ch/pipermail/r-help/2010-February/230065.html). The >>> example he gave was relatively specific and I wanted to generalize his >>> approach into a function. Unfortunately, I seem to have some issues >>> passing the correct arguments to the panel function, and would greatly >>> appreciate any suggestions to solve these issues: >>> >>> require(lattice) >>> >>> mybwplot <- function(x,y,data,groups){ >>> >>> if (missing(groups)||is.null(groups)) { >>> groups <- NULL >>> ngroups <- 1 >>> } else { >>> data[[groups]] <- as.factor(data[[groups]]) >>> ngroups <- nlevels(data[[groups]]) >>> } >>> mywidth <- 1/(ngroups+1) >>> >>> mypanel <- function(x,y,groups,...){ >>> if (missing(groups)||is.null(groups)) { >>> panel.bwplot(x,y,...) >>> } else { >>> panel.superpose(x,y,...) >>> } >>> } >>> >>> mypanel.groups <- function(x,y,groups,ngroups,...){ >>> if (missing(groups)||is.null(groups)){ >>> NULL >>> } else { >>> function(x, y, ..., group.number) { >>> panel.bwplot(x+(group.number-0.5*(ngroups+1))/(ngroups+1),y, ...)} >>> } >>> } >>> >>> bwplot(formula(paste(y,x,s