Search code examples
rsmoothing

Why does R geom_smooth fail (on mpg) when applying linetype=fl?


I’m trying to create multiple geom_smooth-lines using the following code:

# Install necessary packages
install.packages("tidyverse")
library(tidyverse)

# Create the plot
ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = fl, colour = fl, fill = fl))

However, whenever I map it to fl, it fails with the following error message:

Warning messages:
1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :
  span too small.   fewer data values than degrees of freedom.
2: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  :
  at  1.877
3: … : radius  0.000529
4: … : all data on boundary of neighborhood. make span bigger
5: … : pseudoinverse used at 1.877
6: … : neighborhood radius 0.023
7: … : reciprocal condition number  1
8: … : There are other near singularities as well. 21.372
9: … : zero-width neighborhood. make span bigger
10: Computation failed in `stat_smooth()`:
NA/NaN/Inf in foreign function call (arg 5) 

The strange thing is that it works well even when mapping it to something stupendous, such as class (looks like Disney coloured it). It seems quite odd that one shouldn't be able to map it to fuel type, as that is just the kind of data that seems perfect for that kind of study. Is this a bug, or am I missing something completely?

I am running RStudio 0.99.902 and am new to R.


Solution

  • fewer data values than degrees of freedom.

    > table(mpg$fl)
      c   d   e   p   r   
      1   5   8  52 168 
    

    You can't really smooth the single observation in c, you'll need to exclude that or choose another representation.