I have produced two graphs with ggline (ggpubr R package). I would like to merge them into one graph (not side by side, but having the lines produced by the two codes in one graph, similar as two merged layers of an image), but despite searching and trying I have not found a solution yet. Is there a way to merge them directly in R (or to produce similar graphs with other package to be able to merge them)? I know that e.g. for the plot() function, you can add other graphs to the same output by using lines () or points(). Is there a similar way for ggline?
My data look like this (the original question was asked using sample R data ChickWeight, I have now edited all for my data):
Sample Category Individual Value
<chr> <chr> <chr> <dbl>
1 01 E E66 14
2 02 E E66 13.5
3 03 E E66 13.3
4 04 E E66 13.2
5 05 E E66 13.3
6 06 E E66 13.1
7 07 E E66 12.5
8 08 E E66 13.4
9 09 E E66 13.2
10 10 E E66 13.1
11 01 A A13 14.6
12 02 A A13 14.1
13 03 A A13 14.3
14 04 A A13 14.3
15 05 A A13 14.3
16 06 A A13 14.3
17 07 A A13 14.3
18 08 A A13 14.1
19 09 A A13 13.8
20 10 A A13 13.7
21 01 E E62 13
22 02 E E62 12.4
23 03 E E62 12.4
24 04 E E62 12.3
25 05 E E62 12.1
26 06 E E62 13.6
27 07 E E62 12.1
28 08 E E62 12
29 09 E E62 12.5
30 10 E E62 12
31 01 E E65 15.4
32 02 E E65 14.7
33 03 E E65 14.8
34 04 E E65 14.9
35 05 E E65 15.1
36 06 E E65 15.3
37 07 E E65 14.7
38 08 E E65 14.6
39 09 E E65 14.6
40 10 E E65 14.8
41 01 E E69 16.5
42 02 E E69 15.8
43 03 E E69 16
44 04 E E69 15.8
45 05 E E69 15.9
46 06 E E69 16.1
47 07 E E69 15.8
48 08 E E69 15.6
49 09 E E69 15.9
50 10 E E69 15.7
Graph 1
ggline(data, x = "Sample", y = "Value", color = "Category",
add = c("mean_sd"),
palette = c("red", "blue", "green")) + geom_hline(yintercept=14.12, linetype="dashed", color = "black")
Graph 2
ggline(data, x = "Sample", y = "Value", color = "Individual", palette = "grey", point.size=1.5, shape = "Category")
I tried to use the code from the answer, but I received an error: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
Then I added group=1, i.e.
ggplot(data, aes(x = Sample, y = Value, group=1)) +
geom_path(aes(color = Category), alpha = 0.5) +
stat_summary(aes(color = Category), fun.y = "mean", geom = "line", size =1) +
stat_summary(aes(color = Category), fun.y = "mean", geom = "point",size =2) +
stat_summary(aes(color = Category), fun.data = "mean_sd", geom = "errorbar", width = 0.3,size =1) +
geom_hline(yintercept=14.2, linetype="dashed", color = "black") +
theme(legend.position = "top")
But the graph is still not correct, standard deviation (SD) bars for only 1 category (not two) and the Individual lines in the background do not correspond to Graph 2.
I tried to change the Category to numbers 1, 2 instead of A, E and Individuals to 66, instead of E66 and use as.character and as.numeric to define properly the columns, but the background lines were still not as they should have been, I only managed to have SD bars for two categories.
Thank you in advance and let me know, if you need more information.