R:stat_smooth组(x轴)

Jef*_*hen 8 r smooth stat ggplot2

我有一个数据库,想要使用stat_smooth显示一个数字.

我可以显示avg_time vs Scored_Probabilities数字,如下所示:

c <- ggplot(dataset1, aes(x=Avg.time, y=Scored.Probabilities))
c + stat_smooth()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是当将Avg.time更改为时间或Age时,会发生错误:

c <- ggplot(dataset1, aes(x=Age, y=Scored.Probabilities))
c + stat_smooth()
error: geom_smooth: Only one unique x value each group. Maybe you want aes(group = 1)?
Run Code Online (Sandbox Code Playgroud)

我该怎么办呢?

Mam*_*zal 10

错误消息说要设置group=1,这样做会产生另一个错误

ggplot(dataset1, aes(x=Age, y=Scored.Probabilities, group=1))+stat_smooth()
geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
Error in smooth.construct.cr.smooth.spec(object, data, knots) : 
  x has insufficient unique values to support 10 knots: reduce k.
Run Code Online (Sandbox Code Playgroud)

现在,唯一x值的数量是不够的.

所以有两种解决方案:i)使用另一种功能,例如mean,ii)使用抖动稍微移动年龄.

ggplot(dataset1, aes(x=Age, y=Scored.Probabilities, group=1))+
geom_point()+
stat_summary(fun.y=mean, colour="red", geom="line", size = 3) # draw a mean line in the data
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

要么

ggplot(dataset1, aes(x=jitter(as.numeric(as.character(Age))), y=Scored.Probabilities, group=1))+
geom_point()+stat_smooth() 
Run Code Online (Sandbox Code Playgroud)

注意使用as.numeric因为Age是一个因素.

在此输入图像描述