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]]

______________________________________________
[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.

Reply via email to