[R] How to stratify data with multiple group simultaneously with R ggplot2?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Jun 13 13:14:18 CEST 2025


Às 07:41 de 13/06/2025, Luigi Marongiu escreveu:
> Thank you, facets are good but I was looking for something that could
> merge everything in a single plot. I found that I could create an
> additional column, say M (merge), that accommodates two parameters
> into one, for instance, `a + A`, `a + B` etc. By grouping for M, I can
> kind of stratify the data...
> 
> On Sun, Jun 8, 2025 at 6:03 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:
>>
>> Às 07:21 de 08/06/2025, Luigi Marongiu escreveu:
>>> I would like to plot multivariate data with ggplot2. I have multiple
>>> groups that I need to account for: I have the `x` and `y` values, but
>>> the data must be stratified also by `w` and `z`. I can group by either
>>> `w` or `z`, but how can I group for both simultaneously?
>>> In essence, in the example below, the legend should have two columns
>>> (A and B) and five rows (which are already there since the data is
>>> stratified by w). There should be 10 colors.
>>> How can I do that?
>>> Thank you
>>>
>>>>>>>>>
>>> ```
>>> df = data.frame(x = c(rep(1,5), rep(2,5), rep(3,5), rep(4,5)),
>>>                                   w = rep(letters[1:5],4),
>>>                                   z = c(rep(LETTERS[1],10), rep(LETTERS[2],10)),
>>>                                   y = rnorm(20),
>>>                                   stringsAsFactors = FALSE)
>>> library(ggplot2)
>>> ggplot(df, aes(x=x, y=y, colour=w, group=w)) +
>>>     geom_line(linewidth=2) +
>>>     ggtitle("A+B")
>>> ggplot(df[df$z=="A",], aes(x=x, y=y, colour=w, group=w)) +
>>>     geom_line(linewidth=2) +
>>>     ggtitle("A")
>>> ggplot(df[df$z=="B",], aes(x=x, y=y, colour=w, group=w)) +
>>>     geom_line(linewidth=2) +
>>>     ggtitle("B")
>>> ```
>>>
>>> ______________________________________________
>>> 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.
>> Hello,
>>
>> You can use the 4th variable to define facets. Like this?
>>
>>
>> ggplot(df, aes(x=x, y=y, colour=w)) +
>>     geom_line(linewidth=2) +
>>     facet_wrap(~ z, scales = "free_x") +
>>     ggtitle("A+B")
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>>
>> --
>> Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
>> www.avg.com
> 
> 
> 
Hello,

Any of Jeff's two suggestions seems to work well.
(I prefer the first one.)



library(ggplot2)
library(dplyr)

ggplot(df, aes(x = x, y = y, colour = w, linetype = z)) +
   geom_line(linewidth = 2) +
   ggtitle("A+B")

df %>%
   mutate(M = interaction(w, z)) %>% # arrange(M) %>% print()
   ggplot(aes(x=x, y=y, colour=w, group = M)) +
   geom_line(linewidth=2) +
   ggtitle("A+B")



Hope this helps,

Rui Barradas



More information about the R-help mailing list