5

I am a relative novice to R & ggplot. I am trying to plot an interaction. When I plot the interaction using SPSS - the regression lines go from the y-axis all the way to the opposite edge of the plot:

enter image description here

However, when I use ggplot, the regression lines only go as far as the first and last data point which makes the graph look strange

enter image description here

Is there any way to remedy this and make my ggplot look more like the SPSS plot?

Here is the code I am using

No5Plot <- ggplot(data=pccdata, aes(x= factor2, y = pcc, color = Factor.1)) +
  geom_point(size=1.2) +
  theme_classic() +
  geom_smooth(method = "lm", se = F) +
  labs(x = "Factor 2", y = "Posterior Cingulate Cortex \n Gyrification")
No5Plot + scale_color_manual(values = c("#CC79A7", "#009E73", "#0072B2")) + 
    theme(axis.title = element_text(size = 20), axis.text = element_text(size=15))
stefan
  • 90,330
  • 6
  • 25
  • 51
SCC
  • 53
  • 2
  • 1
    Welcome to SO! To help us to help you would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Feb 19 '22 at 11:33
  • 2
    Try with `geom_smooth(..., fullrange=TRUE`). See [ggplot2: Extend stat_smooth regression line to entire x-range of plot area](https://stackoverflow.com/questions/43526124/ggplot2-extend-stat-smooth-regression-line-to-entire-x-range-of-plot-area) – stefan Feb 19 '22 at 11:34

1 Answers1

2

geom_smooth has a fullrange option, which has a default value of FALSE:

Should the fit span the full range of the plot, or just the data?

Thus, you can use:

geom_smooth(method = "lm", se = FALSE, fullrange = TRUE)
Robert Long
  • 5,722
  • 5
  • 29
  • 50