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

Michael Friendly friendly at yorku.ca
Thu Apr 17 04:03:12 CEST 2014

```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) :
>
> gg + coord_trans(ytrans=probability_trans("logis"))
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
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