1

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")

Graph1

Graph 2

   ggline(data, x = "Sample", y = "Value", color = "Individual", palette = "grey", point.size=1.5, shape  = "Category")

Graph2

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.

Graph3

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.

dark
  • 13
  • 3
  • 1
    What do you mean by "merge", do we want to put 2 plots side by side? – zx8754 Jul 22 '19 at 10:06
  • Maybe look into [cowplot::plot_grid](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html) – zx8754 Jul 22 '19 at 10:08
  • Thank you for your answer, I will have a look. – dark Jul 22 '19 at 11:18
  • By merging, I meant having just one graph with the lines produced by the two codes in that single graph. E.g. if I produce the first graph, then add the individual chicken lines (from the second code) to that same first graph (or do it in one step). (So not side by side). – dark Jul 22 '19 at 11:26
  • 1
    `"Chick"` has 50 levels. ggplot has only 11 different line types. Thus, how we should handle this? Please inspect your data and post a scheme (e.g. using paint) to show what you want to illustrate. – Roman Jul 22 '19 at 11:40
  • I'd convert from wide-to-long, then use basic ggplot2, (not a ggpubr). – zx8754 Jul 22 '19 at 11:44
  • 1
    @Jimbou this is just an example dataset, real dataset might be OK with less factor levels. – zx8754 Jul 22 '19 at 11:45
  • @Jimbou, yes, it is an example dataset, I mention, I have less individuals/factor levels. – dark Jul 22 '19 at 12:29
  • OK. but whats about your second graph. Isn't it already illustrating exactly what you want? – Roman Jul 22 '19 at 13:00
  • If you mean, you have the data in two different dataframes, just use "native" ggplot and add a second `geom_line` with `geom_line(data=second_dataset)` perhaps you will have to adjust aesthetics calls. If you provide the first few lines of your real data per `dput` we could get you started with a native `ggplot` https://stackoverflow.com/a/5963610/1842673 – TobiO Jul 22 '19 at 13:44
  • @Jimbou No, because I would like to plot the means of each group (here "diet") with SE and a line showing threshold, here e.g. at weight = 100 (first graph), together with all individual lines (here "Chicks"). So the individuals are kind of background info of the mean. – dark Jul 22 '19 at 14:09

1 Answers1

0

You can try this....I excluded Chick levels to have not more than 11 levels, which can be plotted using linetype

set.seed(123)
ChickWeight %>% 
    filter(Chick %in% sample(1:50, 11) ) %>% 
ggplot(., aes(x =Time, y =weight)) +
  geom_line(aes(linetype = Chick, color = Diet)) + 
  stat_summary(aes(color = Diet), fun.y = "mean", geom = "line") +  
  stat_summary(aes(color = Diet), fun.y = "mean", geom = "point") +  
  stat_summary(aes(color = Diet), fun.data = "mean_se", geom = "errorbar", width = 0.3) +
  theme(legend.position = "top")

Since the Chick information is not really visible and helpful, I recommend something like this:

ggplot(ChickWeight, aes(x =Time, y =weight)) +
  geom_path(aes(color = Diet), alpha = 0.5) + 
  stat_summary(aes(color = Diet), fun.y = "mean", geom = "line", size =1) +  
  stat_summary(aes(color = Diet), fun.y = "mean", geom = "point",size =2) +  
  stat_summary(aes(color = Diet), fun.data = "mean_se", geom = "errorbar", width = 0.3,size =1) +
  theme(legend.position = "top")

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49
  • Thank you, this is exactly the output I need, I am going to try with my real dataset – dark Jul 22 '19 at 14:34
  • I tried with my data, but I did not manage to have an output I wanted. I edited the original question to explain, what I did and also included my data as an example. I also changed a bit the two input codes, so if possible use them for an answer. I would say, some of the problems are related to the format of the data. – dark Aug 13 '19 at 14:55