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?