[R] how to change strip text of effect plot

Joshua Wiley jwiley.psych at gmail.com
Wed Jan 12 06:06:59 CET 2011


Hmm, I felt like it was the clearest, most direct way.  Then again,
things of this nature (overide defaults using arguments rather than
changing what you feed the functions) do seem to be a common request
both for lattice and ggplot2.  In any case, my memory of the lattice
book and a quick search of the archives suggest that the typical way
to do this when dealing with lattice functions such as xyplot() would
be to define a custom strip function.  For example:

strip = function(..., factor.levels) strip.default(..., factor.levels
= c("low", "medium", "high"))

however, if I am not mistaken, you are plotting an object of class
"efflist", which dispatches plot.efflist which eventually dispatches
plot.eff, in the bowels of plot.eff is the call to xyplot(), with its
own strip function already defined.  I did not see a way to override
this, nor did I see an option to pass an argument to it.

It is trivial to edit the function's code to work with such a change.
You could probably create a local copy of it, make the necessary
changes, and then just ensure that your object gets dispatched to your
revised function rather than the packages original.  Even easier, I
suppose:

debug(plot)
plot(eff.cowles, 'neuroticism:ex2',factor.names=F)
## follow along until it has just created the object "plot"
## then overwrite that with your desired changes (factor.levels)
## before it is printed to the screen

    plot <- xyplot(eval(parse(text = paste("fit ~", predictors[x.var],
        "|", paste(predictors[-x.var], collapse = "*")))), strip =
function(..., factor.levels) strip.default(..., factor.levels =
c("low", "medium", "high"),
        strip.names = c(factor.names, TRUE)), panel = function(x,
        y, subscripts, x.vals, rug, lower, upper, has.se, ...) {
        llines(x, y, lwd = 2, col = colors[1], ...)
        if (rug)
            lrug(x.vals)
        if (has.se) {
            llines(x, lower[subscripts], lty = 2, col = colors[2])
            llines(x, upper[subscripts], lty = 2, col = colors[2])
        }
        if (has.thresholds) {
            panel.abline(h = thresholds, lty = 3)
            panel.text(rep(current.panel.limits()$xlim[1], length(thresholds)),
                thresholds, threshold.labels, adj = c(0, 0),
                cex = 0.75)
            panel.text(rep(current.panel.limits()$xlim[2], length(thresholds)),
                thresholds, threshold.labels, adj = c(1, 0),
                cex = 0.75)
        }
    }, ylim = ylim, ylab = ylab, xlab = if (missing(xlab))
        predictors[x.var]
    else xlab, x.vals = x.vals, rug = rug, main = main, lower = x$lower,
        upper = x$upper, has.se = has.se, data = x, scales = list(y =
list(at = tickmarks$at,
            labels = tickmarks$labels), alternating = alternating),
        ...)

alternately still, you could rename the factor levels in the object
"eff.cowles", which is sort of inbetween changing your data and
changing the strip label defaults.

I can understand why it seems like there should be a simpler solution,
but I honestly do not see one.  The factor.levels argument does not
even work inside xyplot(), it needs to be in the strip function, and
that is nested far away from plot(effobject).  I do not see any
documentation that suggests a way, nor do I see any formal arguments
to facilitate it.  Then again, I'm not an expert in lattice or the
effects package.

Best regards,

Josh

On Tue, Jan 11, 2011 at 8:06 PM, Wincent <ronggui.huang at gmail.com> wrote:
> Sure that is one way to go.
> Since it is possible to pass lattice arguments to that high level
> function, and there should be ways to relabel/custom the strip text in
> the lattice plotting system, I would think such an indirect method a
> last resort.
>
> Thank you.
>
> Ronggui
>
> On 12 January 2011 11:51, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>> Hi,
>>
>> I am guessing this is not what you meant by "on the fly", but I think
>> it will be by far the easiest way.  Plotting an effects object is a
>> high level plot with a lot of defaults and automation built in to make
>> your life simple.  The cost is that it is less flexible---you work its
>> way, not vice versa.  If you want the factor named high, just label it
>> that way to begin with.  If you think it makes the graphs more
>> interpretable/meaningful, then it will make model summaries, etc.
>> better also.  Worst case, you fit the model twice (one with fancy
>> names, one with numbers), which unless you have a massive dataset will
>> not take long or be an onerous burden anyways.  Here's how you can
>> include labels directly in cut():
>>
>> Cowles$ex2 <- cut(Cowles$extraversion, 3, c("low", "medium", "high"))
>> mod.cowles <- glm(volunteer ~ sex+neuroticism*ex2,data=Cowles, family=binomial)
>> eff.cowles <- allEffects(mod.cowles)
>> plot(eff.cowles, 'neuroticism:ex2',factor.names=F)
>>
>> Cheers,
>>
>> Josh
>>
>> On Tue, Jan 11, 2011 at 7:21 PM, Wincent <ronggui.huang at gmail.com> wrote:
>>> Dear r heper,
>>>
>>> How can I change the strip text, for example (16,23] in the following
>>> example, to other more informative text such as "high level" on the
>>> fly?
>>>
>>> library(effects)
>>> Cowles$ex2 <- cut(Cowles$extraversion,3)
>>> mod.cowles <- glm(volunteer ~ sex+neuroticism*ex2,data=Cowles, family=binomial)
>>> eff.cowles <- allEffects(mod.cowles)
>>> plot(eff.cowles, 'neuroticism:ex2',factor.names=F)
>>>
>>> Thank you.
>>>
>>> Ronggui
>>>
>>>
>>> --
>>> Wincent Ronggui HUANG (Ph.D.)
>>> City University of Hong Kong
>>> http://asrr.r-forge.r-project.org/rghuang.html
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>>
>
>
>
> --
> Wincent Ronggui HUANG (Ph.D.)
> City University of Hong Kong
> http://asrr.r-forge.r-project.org/rghuang.html
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list