[R] ggplot2: using coord_trans for logit -> probability

Michael Friendly friendly at yorku.ca
Thu Apr 17 14:44:38 CEST 2014


I know I can do that.  My example was just a toy version of a more 
complex graph I
generate on the logit scale, and save as gg.
I wanted to know if there was a way to transform it to the probability 
scale by using

gg + coord_trans()
with some suitable argument(s)

for example, this *does* work to transform x -> log(x)

gg + coord_trans(x="log")

Reading the documentation in scales, I tried

plogis_trans <- function () {
     probability_trans("logis")
}

but I get errors when I try to use it:

 > gg + coord_trans(y="plogis")
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In qfun(x, ...) : NaNs produced



-Michael

On 4/17/2014 6:23 AM, ONKELINX, Thierry wrote:
> Dear Michael,
>
> You can use geom_smooth directly.
>
> ggplot(pred, aes(x = Age, y = Better)) + geom_smooth(method = "glm", family = binomial)
>
> Best regards,
>
> Thierry
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
> + 32 2 525 02 51
> + 32 54 43 61 85
> Thierry.Onkelinx at inbo.be
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Michael Friendly
> Verzonden: donderdag 17 april 2014 4:03
> Aan: R-help
> Onderwerp: [R] ggplot2: using coord_trans for logit -> probability
>
> I'm trying to see if & how I can use coord_trans() with ggplot2 to transform the Y axis of a plot on the logit scale to the probability scale, as opposed to  recalculating everything "manually" and constructing a new plot.
> Here is a simple example of the 'base' plot I'd like to transform:
>
> data(Arthritis, package="vcdExtra")
> Arthritis$Better <- as.numeric(Arthritis$Improved > "None") arth.logistic <- glm(Better ~ Age, data=Arthritis, family=binomial)
>
> # get fitted values on the logit scale
> pred <- data.frame(Arthritis,
>                      predict(arth.logistic, se.fit=TRUE))
> library(ggplot2)
> library(scales)
> # plot on logit scale
> gg <- ggplot(pred, aes(x=Age, y=fit)) +
>     geom_line(size = 2) + theme_bw() +
>     geom_ribbon(aes(ymin = fit - 1.96 * se.fit,
>                     ymax = fit + 1.96 * se.fit,), alpha = 0.2,  color =
> "transparent") +
>     labs(x = "Age", y = "Log odds (Better)") gg
>
> Things I've tried that don't work:
>
>   > gg + coord_trans(ytrans="logis")
> Error in get(as.character(FUN), mode = "function", envir = envir) :
>     object 'logis_trans' of mode 'function' was not found  >  > gg + coord_trans(ytrans=probability_trans("logis"))
> Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed In addition: Warning message:
> In qfun(x, ...) : NaNs produced
>   >
>
> Doing what I want "manually":
>
> # doing it manually
> pred2 <- within(pred, {
>                prob  <- plogis(fit)
>                lower <- plogis(fit - 1.96 * se.fit)
>                upper <- plogis(fit + 1.96 * se.fit)
>                })
>
>
> gg2 <- ggplot(pred2, aes(x=Age, y=prob)) +
>     geom_line(size = 2) + theme_bw() +
>     geom_ribbon(aes(ymin = lower,
>                     ymax = upper), alpha = 0.2,  color = "transparent") +
>     labs(x = "Age", y = "Probability (Better)")
> gg2
>
>
>
> --
> Michael Friendly     Email: friendly AT yorku DOT ca
> Professor, Psychology Dept. & Chair, Quantitative Methods
> York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
> 4700 Keele Street    Web:   http://www.datavis.ca
> Toronto, ONT  M3J 1P3 CANADA
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
> The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
>


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA




More information about the R-help mailing list