# Sample data for plot 1
data1 <- data.frame(
x = 1:5,
y = c(2, 4, 3, 5, 6),
source = "Data 1"
)
# Sample data for plot 2
data2 <- data.frame(
x = 1:5,
y = c(3, 5, 2, 6, 4),
source = "Data 2"
)
# Combine data
combined_data <- rbind(data1, data2)
# Create the combined plot with facetting by source
combined_plot <- ggplot(combined_data, aes(x = x, y = y)) +
geom_point(aes(color = source), size = 3) +
geom_segment(data = data.frame(
x = data1$x,
y = data1$y,
xend = data2$x,
yend = data2$y,
source = data1$source # Ensure source is consistent across layers
), aes(x = x, y = y, xend = xend, yend = yend), linetype = "dotted",
color = "green") +
facet_wrap(~source, scales = "free", ncol = 2) +
labs(title = "Combined Plot with Connecting Lines", x = "X", y = "Y") +
theme_minimal() +
scale_color_manual(values = c("Data 1" = "blue", "Data 2" = "red"))
print(combined_plot)