Home > Software engineering >  How do I convert a cloglog stats family object to a scale transformation in R?
How do I convert a cloglog stats family object to a scale transformation in R?

Time:09-22

I would like to use cloglog as a scale_y_continuous transformation option in ggplot2; that function is inside binomial(link="cloglog") - how can I easily convert the stats family object to a transformation object?

Ideally as a reusable function in case I need to change the link later.

CodePudding user response:

I think this should do it. It could be improved by creating a custom breaks function, or finding one that already exists (the built-in logit_trans doesn't look any prettier ...)1

library(ggplot2)
library(scales)

cc <- make.link("cloglog") ## don't actually need family() here
cloglog_trans <- function () {
    trans <- function(x) cc$linkfun(x)
    inv <- function(x) cc$linkinv(x)
    trans_new("cloglog", trans, inv)
}

example

dd <- data.frame(x = runif(100), y = runif(100))
ggplot(dd, aes(x, y))  
    scale_x_continuous(trans = cloglog_trans())  
    geom_point()

enter image description here

Alternately, you can do this by definining a complementary Gumbel distribution and using probability_trans:

pcgumbel <- function(x) VGAM::pgumbel(-x,lower.tail = FALSE)
qcgumbel <- function(x) -1*VGAM::qgumbel(x, lower.tail = FALSE)
ggplot(dd, aes(x,y))  
    scale_x_continuous(trans = probability_trans("cgumbel"))  
    geom_point()

1: I would love to have a good probability-scale breaks generator, if someone can point to one - although I would be careful which audiences I was communicating with when using a probability transformation ...

  •  Tags:  
  • r
  • Related