missing connecting lines between observations in ggpaired
I'm trying to visualize two paired experiments. I have two sites and at each site I took measurements of 15 individuals in to distinct periods.
When I plot the sites separately, I get the boxplots with the connecting lines, but when I use facet_grid() to get the two plots in one image, the lines disappear. It seems to be a problem with geom_path, but I haven't been able to solve it.
How to make connecting lines show?
Here goes an example:
library(ggplot2)
library(ggpubr)
dat <-
structure(list(period = c("Period1", "Period1", "Period1", "Period1",
"Period1", "Period1", "Period1", "Period1", "Period1", "Period1",
"Period1", "Period1", "Period1", "Period1", "Period1", "Period2",
"Period2", "Period2", "Period2", "Period2", "Period2", "Period2",
"Period2", "Period2", "Period2", "Period2", "Period2", "Period2",
"Period2", "Period2", "Period1", "Period1", "Period1", "Period1",
"Period1", "Period1", "Period1", "Period1", "Period1", "Period1",
"Period1", "Period1", "Period1", "Period1", "Period1", "Period2",
"Period2", "Period2", "Period2", "Period2", "Period2", "Period2",
"Period2", "Period2", "Period2", "Period2", "Period2", "Period2",
"Period2", "Period2"), id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L),
value = c(206, 237, 229, 283, 272, 258, 245, 285, 204, 204,
279, 205, 281, 221, 226, 181, 263, 191, 263, 236, 213, 216,
248, 174, 199, 290, 197, 302, 230, 216, 277, 313, 336, 298,
387, 340, 385, 321, 327, 283, 277, 311, 319, 307, 373, 250,
279, 291, 286, 433, 403, 452, 352, 293, 316, 321, 306, 382,
318, 344), site = c("SiteA", "SiteA", "SiteA", "SiteA", "SiteA",
"SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA",
"SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA",
"SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA", "SiteA",
"SiteA", "SiteA", "SiteA", "SiteA", "SiteB", "SiteB", "SiteB",
"SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB",
"SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB",
"SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB",
"SiteB", "SiteB", "SiteB", "SiteB", "SiteB", "SiteB")), row.names = c(NA,
-60L), class = "data.frame")
p <- ggpaired(dat, x="period", y="value", group="site",line.color = "gray", line.size = 0.4)+
stat_compare_means(paired = TRUE) +
facet_grid(~site)
p
Solution 1:
Looks like in case of faceting you have to be a bit more specific on setting the id
argument, i.e. you could achieve your desired result by setting id="id"
:
library(ggplot2)
library(ggpubr)
ggpaired(dat, x = "period", y = "value", id = "id", line.color = "gray", line.size = 0.4) +
stat_compare_means(paired = TRUE) +
facet_grid(~site)