为什么在应用linetype = fl时R geom_smooth失败(在mpg上)?

时间:2016-10-07 12:39:12

标签: r smooth smoothing

我正在尝试使用以下代码创建多个geom_smooth行:

# 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))

但是,每当我将其映射到fl时,它都会失败,并显示以下错误消息:

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) 

奇怪的是,即使将它映射到令人惊讶的东西,它也能正常工作,例如class(看起来像迪士尼着色它)。似乎很奇怪,人们不应该将它映射到燃料类型,因为这只是那种似乎适合这种研究的数据。这是一个错误,还是我完全错过了什么?

我正在运行RStudio 0.99.902并且是R的新手。

1 个答案:

答案 0 :(得分:0)

  

数据值少于自由度。

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

你无法在c中平滑单个观察,你需要将其排除或选择其他表示。