[R] ggplot stat smooth and poly

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Thu Apr 2 14:39:53 CEST 2020


Dear all

I am not sure, but I believe that in past it was possible to add smoothing
lines in ggplot even if some group did not have enough points to perform
calculation (although I did not find any version which could deliver it).

Here is the code and data

library(ggplot2)
p <- ggplot(test, aes(x=one, y=two, colour=three))
p+geom_point(size=5)+stat_smooth(method="lm")
***line added to each group

p+geom_point(size=5)+stat_smooth(method="lm", formula=y~poly(x,2))
Warning message:
Computation failed in `stat_smooth()`:
'degree' must be less than number of unique points 
***no line added to any group

test <- structure(list(one = 1:20, two = c(1L, 4L, 9L, 16L, 25L, 36L, 
49L, 64L, 81L, 100L, 121L, 144L, 169L, 196L, 225L, 256L, 289L, 
324L, 361L, 400L), three = c("a", "a", "a", "a", "b", "b", "b", 
"b", "c", "c", "c", "c", "c", "d", "d", "e", "e", "e", "e", "e"
)), class = "data.frame", row.names = c(NA, -20L))

My question:  
Is it possible to add smoothing line just to the groups where it can be
added? I know that I could exclude "d" level from my data but I would prefer
to keep them and add only smoothing lines where they could be computed.

Best regards
Petr


More information about the R-help mailing list