R:stat_平滑组(x轴)
我有一个,想用stat_smooth显示一个图形 我可以显示平均时间与得分概率图,如下所示:R:stat_平滑组(x轴),r,ggplot2,smooth,stat,R,Ggplot2,Smooth,Stat,我有一个,想用stat_smooth显示一个图形 我可以显示平均时间与得分概率图,如下所示: c <- ggplot(dataset1, aes(x=Avg.time, y=Scored.Probabilities)) c + stat_smooth() c错误消息显示设置group=1,这样做会产生另一个错误 ggplot(dataset1, aes(x=Age, y=Scored.Probabilities, group=1))+stat_smooth() geom_smooth:
c <- ggplot(dataset1, aes(x=Avg.time, y=Scored.Probabilities))
c + stat_smooth()
c错误消息显示设置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.
现在唯一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
或
请注意使用as.numeric
,因为年龄是一个因素
stat\u smooth
仅适用于连续变量。您的age
变量是字符变量。我只能假设time
变量也不是保存为时间,而是保存为字符。首先必须以有意义的方式将变量转换为数值。在因子上使用as.numeric(as.character(variable))
,as.numeric(variable)
可能无法返回预期值您可能是对的,请随意编辑答案
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
ggplot(dataset1, aes(x=jitter(as.numeric(as.character(Age))), y=Scored.Probabilities, group=1))+
geom_point()+stat_smooth()