[R] lme: Plot colours based on variable?
Leo Mada
|eo@m@d@ @end|ng |rom @yon|c@eu
Thu Dec 4 00:38:09 CET 2025
Dear Vaclav,
Thank you very much for the suggestion.
I tried to avoid this plot, because it wastes a lot of space. Each of Diet 1, 2 and 3 are visualised on separate rows, with the remaining row empty.
Ben and Deepayan provided some working code. Some fine details still need to be improved, but both versions work well otherwise.
Regarding the image: I wanted to add some nice images to a lecture - a wrap up of the statistics module for medical residents. As doctors do not know a lot of statistics, good images can convey the knowledge much better. I wanted to give them some overview of mixed-effects models and some other (more) advanced topics. Funnily, mid through the lecture I tried to have them recount what variance was; only to learn that they are a new batch of residents and this was their first class.
Unfortunately, I am not familiar with lattice. And the terminology used through the documentation is sometimes confusing as well.
Your suggestion to change the colours may be useful to someone, so I am adding it to the discussion thread.
Many thanks,
Leonard
________________________________
From: V�clav Adamec <vaclav.adamec using mendelu.cz>
Sent: Tuesday, December 2, 2025 1:07 PM
To: Leo Mada <leo.mada using syonic.eu>
Subject: Re: lme: Plot colours based on variable?
Hopefully, this is what you want.
Vaclav
library(nlme)
library(lattice)
plot(BodyWeight)
rat.fit = lme(weight ~ Time + Diet, data = BodyWeight, random = ~ Time|Rat)
plot(rat.fit, weight ~ Time | Rat + Diet, groups = ~Diet, col = 2:4,
panel = function(x, y, groups, subscripts, ...) {
panel.xyplot(x, y, ..., groups = groups, subscripts = subscripts, )
})
________________________________________
Od: R-help <r-help-bounces using r-project.org> za u�ivatele Leo Mada via R-help <r-help using r-project.org>
Odesl�no: �ter� 2. prosince 2025 9:39
Komu: Leo Mada via R-help
P�edm�t: [R] 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]]
______________________________________________
R-help using r-project.org 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]]
More information about the R-help
mailing list