0
votes

As the title says, I'm having some difficulty creating a graph with ggplot2 where paired data points are linked by connecting lines. I keep on running into the error message "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?" and no connecting lines are created. From searching online, it looks like most people who have encountered this error message can fix their issue by setting a group aesthetic within geom_line, but this is not working for me, despite that (as far as I can tell) I do have two observations per group.

Here is the graph I have so far and here is the code I used to create the graph:

plot = ggplot(data = df2_mean, aes(x = type, y = mean, fill = type)) + geom_col() + facet_wrap(df2_mean$GH_JSI, strip.position = 'bottom') + 
  labs(x = element_blank(), y = "Seconds", title = "Cumulative Investigation Time", fill = element_blank()) + 
  theme(axis.text.x = element_blank(), axis.ticks = element_blank(), plot.title = element_text(hjust = 0.5)) +
  geom_errorbar(ymin = df2_mean$mean - df2_mean$SEM, ymax = df2_mean$mean + df2_mean$SEM, width = 0.2) + 
  geom_point(data = df2, mapping = aes(x = df2$type, y = df2$value)) +
  coord_cartesian(ylim = c(0, max(df2$value)))

plot

Here are the dataframes used to make the graph:

> df2
   GH_JSI    value   type        ID IDnumeric
1      GH  88.2216 social 1388035-1  13880351
2      GH 152.1190 social 1388034-1  13880341
3      GH  34.1675 social 1388033-2  13880332
4      GH 150.7840 social 1388034-2  13880342
5      GH 225.4590 social 1388033-3  13880333
6      GH 184.2180 social 1388035-3  13880353
7      GH 149.4160 social 1388033-4  13880334
8      GH  77.6443 social 1388034-4  13880344
9      GH 162.3290 social 1388033-1  13880331
10     GH 110.8780 social 1388036-1  13880361
11     GH 158.4250 social 1388036-2  13880362
12     GH 225.8930 social 1388035-2  13880352
13     GH 217.2840 social 1388036-3  13880363
14     GH  94.8282 social 1388034-3  13880343
15     GH 146.5800 social 1388035-4  13880354
16    JSI 151.6180 social 1302238-1  13022381
17    JSI 127.1270 social 1302235-1  13022351
18    JSI 108.5420 social 1302235-2  13022352
19    JSI  80.6140 social 1302259-2  13022592
20    JSI 185.4190 social 1302235-3  13022353
21    JSI 184.4510 social 1302259-3  13022593
22    JSI 210.8110 social 1302235-4  13022354
23    JSI 185.4190 social 1302259-4  13022594
24    JSI 105.5060 social 1302259-1  13022591
25    JSI 113.2130 social 1302305-1  13023051
26    JSI 193.0930 social 1302305-2  13023052
27    JSI 189.3890 social 1302238-2  13022382
28    JSI 138.9060 social 1302305-3  13023053
29    JSI 151.5180 social 1302238-4  13022384
30    JSI 165.6660 social 1302305-4  13023054
31     GH 122.7890 object 1388035-1  13880351
32     GH  77.4775 object 1388034-1  13880341
33     GH  34.8348 object 1388033-2  13880332
34     GH 126.6270 object 1388034-2  13880342
35     GH  66.2996 object 1388033-3  13880333
36     GH  71.0377 object 1388035-3  13880353
37     GH 112.7790 object 1388033-4  13880334
38     GH 114.9820 object 1388034-4  13880344
39     GH 102.0690 object 1388033-1  13880331
40     GH  43.9439 object 1388036-1  13880361
41     GH  50.8842 object 1388036-2  13880362
42     GH 106.0390 object 1388035-2  13880352
43     GH  46.0127 object 1388036-3  13880363
44     GH  57.4575 object 1388034-3  13880343
45     GH 143.0760 object 1388035-4  13880354
46    JSI 135.0680 object 1302238-1  13022381
47    JSI  54.2543 object 1302235-1  13022351
48    JSI  53.2533 object 1302235-2  13022352
49    JSI 142.2090 object 1302259-2  13022592
50    JSI  30.7975 object 1302235-3  13022353
51    JSI  32.5993 object 1302259-3  13022593
52    JSI  60.0934 object 1302235-4  13022354
53    JSI  57.5909 object 1302259-4  13022594
54    JSI  66.9336 object 1302259-1  13022591
55    JSI  89.5229 object 1302305-1  13023051
56    JSI  31.3981 object 1302305-2  13023052
57    JSI  75.3420 object 1302238-2  13022382
58    JSI 103.7700 object 1302305-3  13023053
59    JSI 133.3670 object 1302238-4  13022384
60    JSI 116.7830 object 1302305-4  13023054

Note that for each "social" observation, there is a corresponding "object" observation with the same ID. These are the data points I would like to link with a connecting line.

> df2_mean
  GH_JSI      mean      SEM   type
1     GH 145.21644 14.49010 social
2     GH  85.08726  9.04591 object
3    JSI 152.75280 10.10692 social
4    JSI  78.86549 10.05670 object

And finally, here is how I attempted to add the connecting lines:

df2$IDnumeric = as.numeric(as.character(gsub("-", "", df2$ID)))
plot + geom_line(data = df2, mapping = aes(x = type, y = value, group = IDnumeric))

I've tried playing around with the arguments to geom_line quite a bit, but to no avail. I'm new to ggplot, so maybe there's something fundamental I'm missing here. Any help would be greatly appreciated!

Edit: Here is the dput output of the dataframes

> dput(df2)
structure(list(GH_JSI = structure(c(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, 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), class = "factor", .Label = c("GH", "JSI"
)), value = c(88.2216, 152.119, 34.1675, 150.784, 225.459, 184.218, 
149.416, 77.6443, 162.329, 110.878, 158.425, 225.893, 217.284, 
94.8282, 146.58, 151.618, 127.127, 108.542, 80.614, 185.419, 
184.451, 210.811, 185.419, 105.506, 113.213, 193.093, 189.389, 
138.906, 151.518, 165.666, 122.789, 77.4775, 34.8348, 126.627, 
66.2996, 71.0377, 112.779, 114.982, 102.069, 43.9439, 50.8842, 
106.039, 46.0127, 57.4575, 143.076, 135.068, 54.2543, 53.2533, 
142.209, 30.7975, 32.5993, 60.0934, 57.5909, 66.9336, 89.5229, 
31.3981, 75.342, 103.77, 133.367, 116.783), type = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("object", 
"social"), class = "factor"), ID = structure(c(24L, 20L, 17L, 
21L, 18L, 26L, 19L, 23L, 16L, 28L, 29L, 25L, 30L, 22L, 27L, 5L, 
1L, 2L, 9L, 3L, 10L, 4L, 11L, 8L, 12L, 13L, 6L, 14L, 7L, 15L, 
24L, 20L, 17L, 21L, 18L, 26L, 19L, 23L, 16L, 28L, 29L, 25L, 30L, 
22L, 27L, 5L, 1L, 2L, 9L, 3L, 10L, 4L, 11L, 8L, 12L, 13L, 6L, 
14L, 7L, 15L), class = "factor", .Label = c("1302235-1", "1302235-2", 
"1302235-3", "1302235-4", "1302238-1", "1302238-2", "1302238-4", 
"1302259-1", "1302259-2", "1302259-3", "1302259-4", "1302305-1", 
"1302305-2", "1302305-3", "1302305-4", "1388033-1", "1388033-2", 
"1388033-3", "1388033-4", "1388034-1", "1388034-2", "1388034-3", 
"1388034-4", "1388035-1", "1388035-2", "1388035-3", "1388035-4", 
"1388036-1", "1388036-2", "1388036-3")), IDnumeric = c(13880351, 
13880341, 13880332, 13880342, 13880333, 13880353, 13880334, 13880344, 
13880331, 13880361, 13880362, 13880352, 13880363, 13880343, 13880354, 
13022381, 13022351, 13022352, 13022592, 13022353, 13022593, 13022354, 
13022594, 13022591, 13023051, 13023052, 13022382, 13023053, 13022384, 
13023054, 13880351, 13880341, 13880332, 13880342, 13880333, 13880353, 
13880334, 13880344, 13880331, 13880361, 13880362, 13880352, 13880363, 
13880343, 13880354, 13022381, 13022351, 13022352, 13022592, 13022353, 
13022593, 13022354, 13022594, 13022591, 13023051, 13023052, 13022382, 
13023053, 13022384, 13023054)), row.names = c(NA, -60L), class = "data.frame")

...

> dput(df2_mean)
structure(list(GH_JSI = structure(c(1L, 1L, 2L, 2L), .Label = c("GH", 
"JSI"), class = "factor"), mean = c(145.21644, 85.08726, 152.7528, 
78.8654866666667), SEM = c(14.4901035586421, 9.04591008912605, 
10.1069213744132, 10.0566997660923), type = structure(c(1L, 2L, 
1L, 2L), .Label = c("social", "object"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))
1

1 Answers

0
votes

It looks like you need to fix your facet_wrap line to facet_wrap(~GH_JSI, strip.position = 'bottom') +. The version in your code suggests the faceting should be done on the GH_JSI column from df2_mean, which shares the same values but is not the same data as the column of the same name from df2.

This code works for me on your sample data:

ggplot(data = df2_mean, aes(x = type, y = mean, fill = type)) +
  geom_col() + 
  geom_path(data = df2, aes(x = type, y = value, group = IDnumeric)) +
  facet_wrap(~GH_JSI, strip.position = 'bottom') + 
 ...

enter image description here