Plot several lines with same color

I want to plot y against t, having several conditions (groupings) and several replicates. I'd like to color code the grouping, and have the replicates within a grouping be the same color.

Easy, if I just map the replicate to e.g. linetype:

library(ggplot2)

data.frame(t = rep(1:10, 4),
           y = 1:40,
           grouping = rep(1:2, each = 20) |> as.factor(),
           replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping, linetype = replicate))

But if I don't want different linetypes, I thought I could just use group, but it doesn't appear to work as I expect. What am I missing?



data.frame(t = rep(1:10, 4),
           y = 1:40,
           grouping = rep(1:2, each = 20) |> as.factor(),
           replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping, group = replicate))

Created on 2024-10-01 with reprex v2.1.0

1 Like

Does this do what you want?

library(ggplot2)

data.frame(t = rep(1:10, 4),
           y = 1:40,
           grouping = rep(1:2, each = 20) |> as.factor(),
           replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping, group = interaction(grouping, replicate)))

Created on 2024-10-01 with reprex v2.1.1

1 Like

Indeed! I hadn't realized that the group argument overwrites the other groupings, thank you!


For anyone interested in exploring further:

library(ggplot2)

gg1 <- data.frame(t = rep(1:10, 4),
           y = 1:40,
           grouping = rep(1:2, each = 20) |> as.factor(),
           replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping))

gg2 <- data.frame(t = rep(1:10, 4),
                  y = 1:40,
                  grouping = rep(1:2, each = 20) |> as.factor(),
                  replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping, linetype = replicate))

gg3 <- data.frame(t = rep(1:10, 4),
                  y = 1:40,
                  grouping = rep(1:2, each = 20) |> as.factor(),
                  replicate = rep(c(1,2,1,2), each = 10) |> as.factor()) |>
  ggplot() +
  geom_line(aes(x = t, y = y, color = grouping, group = replicate))


transformed1 <- ggplot_build(gg1)$data[[1]]
transformed2 <- ggplot_build(gg2)$data[[1]]
transformed3 <- ggplot_build(gg3)$data[[1]]

table(transformed1$colour, transformed1$group)
#>          
#>            1  2
#>   #00BFC4  0 20
#>   #F8766D 20  0
table(transformed2$colour, transformed2$group)
#>          
#>            1  2  3  4
#>   #00BFC4  0  0 10 10
#>   #F8766D 10 10  0  0
table(transformed3$colour, transformed3$group)
#>          
#>            1  2
#>   #00BFC4 10 10
#>   #F8766D 10 10

Created on 2024-10-01 with reprex v2.1.0

1 Like

Thanks for this great work, am still trying to understand your dataframe

Your T = numbers from 1 -10, what is the number 4 there?
I think I still understand Y.
Can you please explain your Data frame?

The best is probably to try running the command and see the result.

The idea is that I'm creating a data frame with 3 variables, t, grouping and replicate (the y is irrelevant). The "time" t has values from 1 to 10, and for each time point, I have a grouping of 1 and 2, and a replicate of 1 and 2. So, I need the data frame to look like this, repeated for each value of t:

t  grouping  replicate
1         1         1
1         1         2
1         2         1
1         2         2

So, in practice, I define t = rep(1:10, times = 4), this is, make the vector 1, 2, ..., 10, and repeat it 4 times. So t looks like:

 [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10  1
[22]  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10

Now, for each value of t, I need 2 that have grouping == 1 and 2 that have grouping == 2, so I use grouping = rep(1:2, each = 20) meaning I take the vector c(1,2), and repeat each element 20 times. So it looks like

       t: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 ...
grouping: 1 1 1 1 1 1 1 1 1  1 1 1 1 1 1 1 1 1 1  1 2 2 2 2 2 2 ...

Finally, I need the replicate, I want it to look like:

        t: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 ...
 grouping: 1 1 1 1 1 1 1 1 1  1 1 1 1 1 1 1 1 1 1  1 2 2 2 2 2 2 ...
replicate: 1 1 1    ...    1  1 2 2     ...     2  2 1 1 ... 

To get that, one way is to say I want 10 1s, then 10 2s, then again 10 times 1 and 10 times 2. Hence rep(c(1,2,1,2), each = 10).

Does that make sense to you?