How to implement guide with `ggplot2` when trying to add a regression line to a scatter plot with multiple groups in r
I've been researching this but I'm trying to debug I'm trying to create a scatter plot using `ggplot2` where I want to include separate regression lines for different groups in my dataset. However, when I attempt to add the regression line using `geom_smooth()`, it only seems to apply to the overall dataset and not to the individual groups. I'm using R version 4.3 and `ggplot2` version 3.4.0. Here's a simplified version of my code: ```R library(ggplot2) library(dplyr) # Sample data data <- data.frame( x = rnorm(100), y = rnorm(100), group = rep(c("A", "B"), each = 50) ) # Attempting to plot with regression lines for each group plot <- ggplot(data, aes(x = x, y = y, color = group)) + geom_point() + geom_smooth(method = "lm", se = FALSE) # This doesn't work as expected print(plot) ``` I expected different regression lines for groups A and B, but I'm just getting a single line that represents the entire dataset. When I try to use `group = group` within `aes()` in `geom_smooth()`, I still get the same result. I also tried using `facet_wrap()` to separate the groups, but that doesn't serve my purpose of overlaying the lines on the same plot. Is there a way to ensure that `geom_smooth()` computes a separate regression line for each group in the scatter plot? Any help would be greatly appreciated! This is my first time working with R 3.9.