How to plot two geom_line (one from each group) into a single Plot in ggplot2

Asked

Viewed 121 times

2

I have 2 dfs (one from each group) that correspond to 76 channels of signal acquisition in 20sec. of analysis. To decrease the work of repeating several times (76x) the geom_line function I stacked the channels using melt(). Send below only the first 5 channels to help in the solution.

  dput(df1)

structure(list(time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), grupo = c("A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L), .Label = c("v1", "v2", "v3", "v4", "v5"), class = "factor"), 
    value = c(7.42, 7.86, 8.37, 7.13, 8.02, 8.87, 9.43, 9.86, 
    10.21, 10.32, 10.2, 9.89, 9.43, 8.9, 8.35, 7.93, 7.33, 6.98, 
    8.66, 8.3, 8.3, 8.4, 8.48, 8.57, 8.62, 8.71, 8.88, 9.09, 
    9.38, 9.69, 10, 10.44, 10.94, 11.44, 9.94, 10.31, 10.57, 
    10.77, 10.82, 10.74, 7.75, 7.7, 7.72, 8.34, 6.79, 7.5, 7.63, 
    7.61, 7.6, 7.56, 7.55, 7.57, 7.6, 7.91, 6.38, 6.65, 7.36, 
    7.5, 8.74, 7.41, 7.61, 7.53, 7.41, 8.76, 10.26, 7.47, 7.37, 
    7.19, 7.05, 7.01, 6.9, 8.78, 8.66, 8.27, 9.71, 9.46, 8.72, 
    8.57, 7.25, 8.53, 11.25, 11.95, 11.56, 11.82, 10.32, 10.18, 
    10.37, 11.29, 9.93, 10.75, 11.44, 11.2, 11.53, 10.35, 10.78, 
    10.74, 9.93, 10, 10.56, 10.87)), row.names = c(NA, -100L), class = "data.frame")

and the other group:

    dput(df2)
structure(list(time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), grupo = c("B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L), .Label = c("v1", "v2", "v3", "v4", "v5"), class = "factor"), 
    value = c(-1.33, -1.34, -1.34, -1.36, -1.34, -1.36, -1.35, 
    -1.37, -1.38, -1.37, -1.36, -1.4, -1.41, -1.4, -1.38, -1.39, 
    -1.42, -1.39, -1.43, -1.43, -3.92, -3.96, -4.04, -4.03, -3.98, 
    -4.16, -4.38, -4.54, -4.62, -4.62, -4.4, -4.16, -3.93, -3.91, 
    -4.02, -4.07, -3.88, -3.24, -3.16, -3.27, -2.53, -2.562, 
    -2.642, -2.616, -2.582, -2.746, -2.974, -3.117, -3.189, -3.197, 
    -2.986, -2.712, -2.474, -2.462, -2.589, -2.631, -2.415, -1.801, 
    -1.687, -1.797, -2.69, -2.26, -2.02, -2.05, -2.19, -2.3, 
    -2.21, -1.97, -1.98, -2.13, -2.06, -2, -2.07, -2.13, -2.15, 
    -2.11, -2.15, -2.19, -2.2, -2.25, -2.617, -2.53, -2.51, -2.514, 
    -2.523, -2.641, -2.728, -2.749, -2.792, -2.829, -2.701, -2.568, 
    -2.471, -2.476, -2.535, -2.55, -2.466, -2.155, -2.119, -2.187
    )), row.names = c(NA, -100L), class = "data.frame")

Plotting the charts for each group was accomplished successfully as below:

    pl <- ggplot(df1, aes(time, value)) +
  geom_line(aes(group = variable)) + geom_smooth(color = "red")


pl2 <- ggplot(df2, aes(time, value)) +
  geom_line(aes(group = variable)) + geom_smooth(color = "blue")

Here’s the problem: I can’t override these 2 Plots to appear together in one graph.

I tried to rbind() to join the groups "A" and "B" into a single df, but when I declare in ggplot, the graph does not match the desired.

What would be the right way to use Aesthetics to get the 2 experiments on the same chart, each with its own geom_smoth()?

1 answer

5


The ggplot2 gets lost in your graph with two groups because, in reality, you have two grouping variables for your problem: grupo (levels A and B) and variable (with levels v1 to v5). One way to solve this is by creating a third variable, which indicates the interaction between them. The function interaction does this perfectly and can be applied within the command itself ggplot:

df <- rbind(df1, df2)

ggplot(df, aes(x = time, y = value, colour = grupo, 
  group = interaction(grupo, variable))) +
  geom_line(aes(alpha = 0.5), show.legend = FALSE) +
  geom_smooth(aes(group = grupo))

inserir a descrição da imagem aqui

I tried to give an improved visualization using alpha = 0.5 to leave a small transparency in the original curves, making the solid color of the geom_smooth stand out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.