1 min read

Connect points in graph divided by facet

To do so, specify group attribute in aes() in ggplot2 expression. This would allow points connected in the subplots defined by facet_wrap() or facet_grid().

Working example:

dat <- data.frame(x = rep(1:3, 4),
                  y = c(1:12), 
                  individual = factor(c(1,1,1, 2,2,2, 3,3,3, 4,4,4)),
                  type = factor(c(rep("A", 6), rep("B", 6)))
)

# without specifying group
dat %>% ggplot(aes(x = x, y = y)) +
        geom_point() + facet_wrap(type ~ .) +
        geom_line()
# specify "individual" as grouping variable
 dat %>% ggplot(aes(x = x, y = y, group = individual)) +
        geom_point() + facet_wrap(type ~ .) +
        geom_line()