R:stat_smooth 组(x 轴)

2023-11-25

我有一个Database,并想使用 stat_smooth 显示图形。

我可以显示 avg_time 与 Scored_Probabilities 的对比图,如下所示:

c <- ggplot(dataset1, aes(x=Avg.time, y=Scored.Probabilities))
c + stat_smooth()

enter image description here

但是当将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)?

我该如何解决它?


错误消息说要设置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) 使用抖动来稍微移动 Age。

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

enter image description here

Or

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

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

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R:stat_smooth 组(x 轴) 的相关文章

随机推荐