R: stat_smooth groups (x axis)

I have a Database and you want to show the shape using stat_smooth.

I can show an avg_time vs Scored_Probabilities figure that looks like this:

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

enter image description here

But when Avg.time changes in time or in Age, an error occurs:

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

How can i fix this?

+6
source share
1 answer

the error message says group=1 , which gives another error

 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. 

Now the number of unique x values ​​is not enough.

So, two solutions: i) using another function, such as mean , ii) using jitter to move a little 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(Age)), y=Scored.Probabilities, group=1))+ geom_point()+stat_smooth() 

Pay attention to the use of as.numeric , because Age is a factor.

enter image description here

+6
source

Source: https://habr.com/ru/post/987111/


All Articles