Home > front end >  Secondary axis in R which reflects percentiles
Secondary axis in R which reflects percentiles

Time:11-08

I am trying to plot T norm scores (M=50, SD=10) and their relative percentiles with a secondary axis in R.

The data frame would look like this:

scale <- c("X","Y","Z") 
tScore <- c(55, 70, 43) 
percentile <- pnorm((tScore-50)/10) 
dataFrame <- data.frame(scale,tScore,percentile)

I tried to play around with the sec.axis function:

ggplot(dataFrame, aes(tScore,scale))   geom_point()  
  scale_x_continuous("T Norms", limits=(c(20,80)),
              sec.axis = sec_axis(~./ pnorm(dataFrame$tScore), name = "Percentile"))

I am trying to get the scale-Names (X,Y,Z) on the left on the y-axis and then three points - as the z-score and the percentile should be the same point - but with two y-axis, one in the top and one in the bottom. I tried in this example to not use the "percentile" column I created (because I would not know how) but to transform directly. Unfortunately, it does not take pnorm as a way of transformation as it seems.

CodePudding user response:

Not 100% if I understand what you are trying to achieve. But to me that's sound like that you want a secondary x scale showing the percentiles for the t-scores plotted on the primary scale. In that case there is no need for a transformation of the scale range. Instead you simply have to transform the labels:

library(ggplot2)

perc_label <- function(x) {
  x <- pnorm((x - 50) / 10)
  scales::label_number(accuracy = .001)(x)
}

ggplot(dataFrame, aes(tScore, scale))  
  geom_point()  
  scale_x_continuous("T Norms",
    limits = c(20, 80),
    sec.axis = dup_axis(
      breaks = dataFrame$tScore,
      labels = perc_label, 
      name = "Percentile")
  )

enter image description here

  • Related