Re: lme: Plot colours based on variable?
Deepayan Sarkar <[email protected]>
| Newsgroups | gmane.comp.lang.r.general |
|---|---|
| Message-ID | <CADfFDC7XTJchA6Z=pcaPB=9EcLMH=_VH0pMA=LxwWYmtXc9erQ@mail.gmail.com> |
On Tue, 2 Dec 2025 at 21:56, Ben Bolker <[email protected]> wrote: > I often find custom-built lattice-based plot methods inscrutable and > end up falling back to figuring out how to get what I want directly with > lattice. > The documentation suggests that this might work, but the colors come out weird. plot(BodyWeight, innerGroups = ~Diet) > > I haven't figured out how to get the label 'Diet' on the legend, but > otherwise this works pretty well ... > > > library(lattice) > library(nlme) > labs <- paste(attr(BodyWeight, "labels"), > attr(BodyWeight, "units")) > xyplot(weight ~ Time | Rat, type = c("p", "r"), > data = BodyWeight, > layout = c(NA, 1), ## enforce 1 row > or aspect = "xy", as in the plot method. > groups = Diet, > auto.key = TRUE, > pch = 19, > xlab = labs[1], > ylab = labs[2], > par.settings = list(superpose.line = list(col = "black"))) > A more direct analog of plot(BodyWeight) would be xyplot(BodyWeight, weight ~ Time | Rat, aspect = "xy", type = "o", groups = Diet, auto.key = list(title = "Diet", columns = 3), grid = TRUE, xlab = labs[1], ylab = labs[2]) -Deepayan > > > On 12/2/25 06:06, Duncan Murdoch wrote: > > On 2025-12-02 3:39 a.m., Leo Mada via R-help wrote: > >> Dear R-Users, > >> > >> Is there a quick solution to plot colours based on a variable? > >> > >> I tried something like this: > >> > >> library(nlme) > >> > >> plot(BodyWeight) > >> > >> # Does NOT work! > >> colDiet = c(2:4)[BodyWeight$Diet] > >> plot(BodyWeight, col = colDiet) > >> > >> # Wastes space > >> rat.fit = lme(weight ~ Time + Diet, data = BodyWeight, random = ~ > >> Time|Rat) > >> plot(rat.fit, weight ~ Time | Rat + Diet) > >> > >> Is there a quick way to base the colour on the Diet variable? > > > > plot(BodyWeight, ...) > > > > dispatches to plot.nfnGroupedData(), which uses lattice::xyplot() > > graphics rather than base graphics. I don't know lattice well enough to > > answer your question, but maybe this will give you a hint... > > > > Duncan Murdoch > > > > ______________________________________________ > > [email protected] mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide https://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > -- > Dr. Benjamin Bolker > Professor, Mathematics & Statistics and Biology, McMaster University > Associate chair (graduate), Mathematics & Statistics > Director, School of Computational Science and Engineering > * E-mail is sent at my convenience; I don't expect replies outside of > working hours. > > ______________________________________________ > [email protected] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]]