[R] lme: Plot colours based on variable?
Leo Mada
|eo@m@d@ @end|ng |rom @yon|c@eu
Tue Dec 2 18:53:44 CET 2025
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 <leo.mada using syonic.eu>
Sent: Tuesday, December 2, 2025 10:39 AM
To: Leo Mada via R-help <r-help using r-project.org>
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]]
More information about the R-help
mailing list