在ggplot2中如何画出较为粗糙的线条?有没有替代geom_smooth的方法?

9
使用以下数据:
> str(attribute)
'data.frame':   431 obs. of  2 variables:
 $ pos: int  1 2 3 4 5 6 7 8 9 10 ...
 $ att: num  0.652 0.733 0.815 1.079 0.885 ...   *[between 0 and 3]

并且:

ggplot(attribute, aes(x=pos, y=att)) + geom_line() + geom_smooth()

我做了以下操作: enter image description here 我希望逐步平滑黑色曲线,而不是像geom_smooth默认所做的那样“非常平滑”。我尝试了n、level选项,但没有达到我想要的效果。如何逐步增加平滑度是最好的方法?(例如将两个值平均在一起,然后尝试将三个值平均在一起,以此类推)。我猜这是一件非常容易或可实现的事情,而无需使用geom_smooth,但我不知道该搜索/查找什么。谢谢。
1个回答

12

这在stat_smooth文档中有记录。默认的平滑器是loess,其他参数会按照...参数的描述传递给它。所以你需要的是span

ggplot(mtcars,aes(x = wt,y = mpg)) + 
  geom_point() + 
  geom_smooth(span = 0.4)

此外,loess 接受一个 degree 参数来更好地控制平滑程度。


degree 看起来已经过时了,但 span 仍然有用。 - geotheory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接