user3654634
user3654634

Reputation: 133

Combine plotted points for several groups with single line of means

I have a series of time points, each with three groups. data points for these groups are graphed as points representing group means with group error bars. In the same graph, I wanted to include a single line through points representing the mean for all three groups combined (I added the mean values to the data frame in the same format as individual data points). The idea is to show that variance was between and not within groups, while still showing the trend in the overall mean in one graph. Unfortunately, I get a separate line for all groups and the overall mean. Here's some data I called mean_copies_all:

   Target.Name Filter Mean_copies_per_ml
1            1      A         529145.308
2            1      B         182360.807
3            1      C         341843.679
4           10      A         849011.671
5           10      B         212161.046
6           10      C         215310.068
7           11      A        1217760.052
8           11      B        1089211.559
9           11      C         601981.345
10          12      A          99735.053
11          12      B          51514.408
12          12      C          51218.250
13          13      A          48987.897
14          13      B          14574.007
15          13      C           7415.393
16          14      A          56187.722
17          14      B          27481.111
18          14      C          53496.085
19          15      A          23620.870
20          15      B          13247.077
21          15      C           7003.142
22          16      A          42031.020
23          16      B           5704.238
24          16      C           2899.576
25           2      A          60440.491
26           2      B         112070.893
27           2      C          57932.772
28           3      B         133824.150
29           3      C         138466.953
30           4      A         271841.956
31           4      B          70476.821
32           4      C         136501.090
33           5      A         557738.584
34           5      B         282025.027
35           5      C         141771.466
36           6      B         329257.875
37           6      C         328892.995
38           7      A         466721.296
39           7      B         471029.030
40           7      C         477953.408
41           8      A         263504.598
42           8      B         118359.180
43           8      C         239695.292
44           9      A         773710.137
45           9      B         781166.768
46           9      C         199823.470
47           1   mean         351116.598
48          10   mean         425494.262
49          11   mean         969650.985
50          12   mean          67489.237
51          13   mean          23659.099
52          14   mean          45721.639
53          15   mean          14623.696
54          16   mean          16878.278
55           2   mean          76814.719
56           3   mean         136145.552
57           4   mean         159606.622
58           5   mean         327178.359
59           6   mean         329075.435
60           7   mean         471901.244
61           8   mean         207186.356
62           9   mean         584900.125    

This is my code:

p <- ggplot(mean_copies_all,aes(y=log10(Mean_copies_per_ml),x=as.numeric(Target.Name)))
p + geom_point(aes(colour=Filter)) + xlab("Sample") + ylab("Gene Copies/mL") + geom_line(aes(colour=Filter,stat="mean"))

I tried using stat_summary, but I kept getting an error saying "'what' must be a character string or function." I couldn't find much info on this error so I stuck with geom_line. Is there a simple way to plot the overall mean line only?

Edit: I included the data and removed the bits about adding the error bars in the code, since it probably wasn't relevant.

Upvotes: 0

Views: 205

Answers (1)

CMichael
CMichael

Reputation: 1866

Ok I realized I misread your original post - I thought you wanted within Filter means...

Since you already prepared the means in your dataset it is really easy:

For sake of clarity I create distinct dataframes outside of the ggplot object - you could do the filtering within as well.

mean_copies_filtered <- mean_copies_all[mean_copies_all$Filter!="mean",]
means <- mean_copies_all[mean_copies_all$Filter=="mean",]

p <- ggplot(mean_copies_filtered,aes(y=log10(Mean_copies_per_ml),x=as.numeric(Target.Name)))
p <- p + geom_point(aes(colour=Filter)) + xlab("Sample") + ylab("Gene Copies/mL")
p + geom_line(data=means,aes(a=as.numeric(Target.Name),y=log10(Mean_copies_per_ml)))

This yields:

enter image description here

Upvotes: 0

Related Questions