panel.subgroups: Panel function to display subgroups within groups within...

View source: R/graphics.R

panel.subgroupsR Documentation

Panel function to display subgroups within groups within panels

Description

This function is designed to be used as the argument to panel.groups in xyplot. It effectively adds another level of subgrouping to that implemented by the groups argument in xyplot. Useful mainly to display data and fitted lines in groups within panels.

Usage

panel.subgroups(
  x,
  y,
  subscripts,
  subgroups,
  subgroups.type = c("p", "l"),
  type,
  panel.subgroups = panel.xyplot,
  ...
)

Arguments

x, y

coordinates of the points to be displayed

subscripts

subscripts giving indices in original data frame

subgroups

a subgrouping variable. Use a full reference, e.g. data$subvar

subgroups.type

plotting type, typically 'p' or 'l', for each level of the variable passed through the subgroups argument

type

FIXME

panel.subgroups

function use to plot data within in each group referring to the levels of the variable passed by subgroups. Define a panel.subgroups argument in the call to xyplot and it will be used to plot the groups. See the examples below.

...

any other arguments to be passed to the panel plotting function

Details

This function is designed to be used as the argument to 'panel.groups' in 'xyplot'. It allows the plotting of points versus lines within subgroups of the data identified by the 'groups' argument in xyplot. It requires a variable to identify the subgroups. Points or lines are used within subgroups depending on 'subgroups.type' where the order is that of the levels of the 'subgroups' argument coerced as a factor, if necessary. See the examples below.

See Also

link[lattice]{panel.superpose}, link[lattice]{panel.superpose.2}, link[lattice]{panel.xyplot}

Examples

## Not run: 
library(car)
data(Prestige)
fit <- lm( prestige ~ (education +I(education^2)) * type, Prestige, na.action = na.omit)
pred <- expand.grid( education = seq( 6, 18, .5), type = levels( Prestige$type))
pred$prestige <- predict( fit, newdata = pred )

Prestige$what <- 'data'
pred$what <- 'fit'         # this works because 'fit' follows 'data' lexicographically

combined <- merge( Prestige, pred, all = T)

xyplot( prestige ~ education, combined,
          groups = type,
          subgroups = combined$what,  # note that a full reference to the variable is needed
          panel = panel.superpose,    # might not be necessary in future version of lattice
          panel.groups = panel.subgroups)  # uses the default of points for the first level of 'what'
                                           # and lines for the second level

## Using the argument 'panel.subgroups' instead of the default 'panel.xyplot'
## Note that panel.subgroups is a function (this one) and also an argument that
## is a function passed to the function. The argument defines the action to
## be taken within each level of 'what'

xyplot( prestige ~ education, combined,
        groups = type,
        subgroups = combined$what,  # note that a full reference to the variable is needed
        panel = panel.superpose,    # might not be necessary in future version of lattice
        panel.groups = panel.subgroups,
        panel.subgroups = function( x, y, subgroup, type, ...) {
             # note that you need to include 'type' among the arguments
             # if you need to prevent it from being passed through '...'
             # When called, this function will be passed arguments
             # subgroup, subgroup.number, subscripts, and type from
             # subgroups.type.
       if ( subgroup == 'data' ) {
              panel.xyplot( x, y, ...)
              panel.lines( dell(x,y), ...)
       } else {
              panel.lines( x,y, ...)
       }
   })

## End(Not run)

gmonette/spida2 documentation built on Aug. 11, 2024, 7:52 p.m.