R plot logit transformed data on normal axis (logistic transformed)

Viewed 619

I would like to plot logit transformed data (originally percentages) in a comprehensible manner, i.e., in a plot with a logistic transformed y-axis.

library(tidyverse)

set.seed(123)

# Logit transformation function
logit <- function(x){
  x <- x/100
  log(x/(1-x))
}

# Create sample data
df <- data.frame(x = seq(1,10,1), 
           y = runif(10, 0, 10)) %>% 
  mutate(y = logit(y))

# Plot
ggplot(df, aes(x,y)) +
  geom_point()

I noticed that the scale_y_continuous function has a trans argument, which should be able to do the job. However, a logit function is built-in, but its opposite the logistic isn't.

How can I plot logit transformed data normally (i.e., with a logistic transformed y-axis)?

1 Answers

If the data is in percentage and you want the y-axis to be logit transformed, with the y axis labels staying as percentage, you can create a new trans class from scales, in the example below, i use qlogis which is the equivalent of your logit function:

library(scales)
logit_perc <- trans_new("logit perc",
transform = function(x)qlogis(x/100),
inverse = function(x)100*plogis(x)
)

set.seed(1001)
df <- data.frame(x = seq(1,10,1),y = runif(10, 0, 10)) 

ggplot(df,aes(x,y)) + geom_point() + coord_trans(y = logit_perc)

enter image description here

If you have already the values in logit, and you want to plot in logit scale with labels in percentages:

df = df %>% mutate(y=logit(y))

# Plot
ggplot(df, aes(x,y)) + geom_point() + 
scale_y_continuous(labels=function(x)round(100*plogis(x),digits=1))

enter image description here

while the original plot looks like:

ggplot(df, aes(x,y)) + geom_point()

enter image description here

Related