You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice if we could support nested grouping. (Or, put differently, allow colours to vary/repeat across units.) This would mostly be useful for line plots where we want to avoid joining the end of one line with the start of another. The idea is similar to how ggplot2 allows you to specific aes(col = var1, group = var2) separately.
Here is an illustration using the following dataset. The setting is a difference-in-differences research design with staggered treatment. So we have treatment cohorts (first_treat) superimposed on individual units (id).
First, points. (Fine.)
plot2(y~time|first_treat, dat)
Second, lines. (Not fine, because we have lines rejoining across units in the same cohort.)
plot2(y~time|first_treat, dat, type="l")
Of course, we could group (colour) by the individual IDs. This stops the rejoining, but means that we lose the colouring by treatment group (which is the interesting thing from a causal inference perspective).
plot2(y~time|id, dat, type="l", legend=FALSE)
I don't have a solution right now, but it probably requires a new argument like bycol. On the formula side, we could potentially represent this via a / nesting interaction. So the call would become plot2(y ~ time | first_treat / id, dat, type = "l"), i.e. units are nested within first treatment cohorts.
Ran into this again recently and am now thinking a simpler solution is just to support passing a variable to col. It should be pretty simple to grab the corresponding colour breaks and pass them to our group-split data, by using something like tapply(factor(col_var), by_var, FUN = [[, 1) internally.
Just wanted to point out that with the recently added color recycling #410 the following does work:
plt(y ~ time | first_treat + id, dat, type = "l", col = palette.colors(5, "R4"), legend = FALSE)
This is reasonably short, hence I thought I'd post it. But it still yields a warning. More importantly, it is not so obvious that this groups the colors by first_treat and the legend cannot be auto-generated either.
Activity
grantmcdermott commentedon Aug 30, 2024
Ran into this again recently and am now thinking a simpler solution is just to support passing a variable to
col. It should be pretty simple to grab the corresponding colour breaks and pass them to our group-split data, by using something liketapply(factor(col_var), by_var, FUN =[[, 1)internally.Manual proof of concept:
Created on 2024-08-30 with reprex v2.1.1
TBD on how to handle legends, as well as NSE vs formula arguments.
zeileis commentedon May 23, 2025
Just wanted to point out that with the recently added color recycling #410 the following does work:
This is reasonably short, hence I thought I'd post it. But it still yields a warning. More importantly, it is not so obvious that this groups the colors by
first_treatand the legend cannot be auto-generated either.