Re: lme: Plot colours based on variable?

Leo Mada via R-help <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <DBAP192MB09566E3BD37F4B8539E43DC984D8A@DBAP192MB0956.EURP192.PROD.OUTLOOK.COM>
Dear Ben,

Thank you very much for the suggestion. Indeed, it works.

Many thanks to Duncan and Deepayan for the other suggestions, as well.

I managed to add the legend title (figured out that legend is named "key"). The code looks like this (though the pch, fill & type-options do not work yet, and as much as I fiddle with them, I don't get them set):

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
        groups = Diet,
        auto.key = list(title = "Diet", pch = 16, type = "n", fill = 0.6),
        pch = 19,
        xlab = labs[1],
        ylab = labs[2],
        par.settings = list(superpose.line = list(col = "black")))

The code provided by Deepayan is much shorter; although the colours are not the best choice.

Sincerely,

Leonard

________________________________
From: Leo Mada <[email protected]>
Sent: Tuesday, December 2, 2025 10:39 AM
To: Leo Mada via R-help <[email protected]>
Subject: lme: Plot colours based on variable?

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?

Many thanks,

Leonard


	[[alternative HTML version deleted]]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.