Search code examples
rggplot2linear-regression

When using geom_smooth to plot a best fit line I get: `stat_smooth()`: invalid 'x' type in 'x || y' in R


I am currently trying to create a scatterplot with a best-fit line.

df %>% 
  ggplot(aes(y = d_emp, x=d_gdp)) +
  geom_point() +
  geom_smooth(method = "lm", se = "FALSE")

However, I keep getting the following error message:

Computation failed in stat_smooth(): invalid 'x' type in 'x || y'

The scatterplot works perfectly, it is only the best fit line that does not show (so the geom_smooth part).

enter image description here

Here is what my dataset looks like:

My data


Solution

  • You provided a string to the boolean argument se. This should work

    df %>% 
      ggplot(aes(y = d_emp, x = d_gdp)) +
      geom_point() +
      geom_smooth(method = "lm", se = FALSE)