r – Connecting geom_line with dodged points within a point grouping, within a facet_wrap

Edit2:

You could create an extra column with the x-axis position in numeric way to have the values dodged. You could use two x aesthetic columns for both the layer and geom_point and geom_line. Using scale_x_continuous to have the continuous axis to dodge the values and create labels like this:

library(ggplot2)
library(stringr)
library(dplyr)

df %>%
  mutate(Treatment2 = c(0.8, 1.2, 2.8, 3.2, 1.8, 2.2, 3.8, 4.2)) %>%
  ggplot(aes(x=Treatment, y=Ratio, shape=Year, linetype=factor(Treatment))) +
  geom_point(aes(x = Treatment2), size=3.5)+
  geom_line(aes(group = Treatment, x = Treatment2)) +
  scale_x_continuous(breaks = c(1,2,3,4), labels = c("Drill_Herb", "Herb_None", "Drill_None", "Herb_Drill")) +
  facet_wrap(~Comparison_Combo, drop = TRUE, scales = "free_x", nrow = 1) 

Created on 2023-01-31 with reprex v2.0.2

Edit

You should add a column that shows the combination of the points you want to connect like this:

library(ggplot2)
library(stringr)
library(dplyr)

df %>%
  mutate(Treatment2 = c(1, 2, 3, 4, 1, 2, 3, 4)) %>%
  ggplot(aes(x=Treatment, y=Ratio, shape=Year, linetype=factor(Treatment2), group = factor(Treatment2))) +
  facet_wrap(~Comparison_Combo, drop = TRUE, scales = "free_x", nrow = 1) +
  geom_point(size=3.5, position=position_dodge(width=0.55))+
  geom_path(lwd=1, 
            position=position_dodge(width=0.55))

Created on 2023-01-31 with reprex v2.0.2


You could use group in your aesthetics like this:

library(ggplot2)
library(stringr)
ggplot(df, aes(x=Treatment, y=Ratio, shape=str_wrap(Year,25), 
               linetype=str_wrap(Treatment), group = 1)) +
  facet_wrap(~Comparison_Combo, drop = TRUE, scales = "free_x", nrow = 1) +
  geom_point(size=3.5, position=position_dodge(width=0.55))+
  geom_path(aes(group=Treatment), lwd=1, 
            position=position_dodge(width=0.55))

Created on 2023-01-31 with reprex v2.0.2

Read more here: Source link