Re: [R] ggplot stat smooth and poly

2020-04-05 Thread Deepayan Sarkar
On Thu, Apr 2, 2020 at 6:10 PM PIKAL Petr  wrote:

> 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.
>

Looks like there's a tryCatch around each panel, but not for each group
within panel. So this would work:

p + geom_point(size=2) + facet_wrap(~three) +
stat_smooth(method="lm", formula=y~poly(x,2))

but one problematic group is enough to make a whole panel fail.

Other than rewriting StatSmooth$compute_panel to protect each per-group
call, a workaround could be to replace method="lm" by a safe wrapper, e.g.,:

plm <- function(formula, data, ...)
{
ocall <- match.call(expand.dots = TRUE)
ocall[[1]] <- quote(lm)
fm <- try(eval(ocall, parent.frame()), silent = TRUE)
if (inherits(fm, "try-error"))
{
ocall[[2]] <- y ~ x
fm <- eval(ocall, parent.frame())
}
fm
}

p + geom_point(size=2) + stat_smooth(method=plm, formula=y~poly(x,2))

-Deepayan

Best regards
> Petr
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] ggplot stat smooth and poly

2020-04-02 Thread PIKAL Petr
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
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.