Show density like plot but include Z Scores and coresponding actual values

This post may be wishful thinking, I wanted to ask here before I attempt to reinvent a wheel that may already be made.

I have been asked to report on thresholds on a distribution of spend data which is log normally distributed. I would like to plot the distribution and then annotate it with the Zscores along with the corresponding exponentiated actual values of the original, non log transformed distribution.

I did some image searching online and could not find anything showing exactly what I need, but I'll try with this image from wikicommons:

I can get the curve itself using ggplot::geom_density. Is there a pre built way to also overlay the Zscores and then, perhaps in a table underneath, the corresponding actual values of those Zscores based on the underlying distribution?

mtcars %>% ggplot(aes(x = log(mpg))) + geom_density()

s <- mtcars$mpg %>% log %>% summary
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.342   2.736   2.955   2.958   3.127   3.523

With the above, I know that the mean corresponds to s[['Mean']] %>% exp = 19.25006 so I would like a vertical line down the middle showing the value 19.2. That would correspond with a Zscore of 0 based on the first image I pasted above. But I would also like the corresponding values for the +- 1, 2 and 3 z-scores.

Is there a conventional way of annotating a distribution plot in this way? Is there a out of the box function, like summary() that can show me the z-scores of a distribution and their corresponding values?

some_function_i_hope_exists(mtcars$mpg %>% log)
# returns a table showing zscores and corresponding actual values
mlmm <- mean(log(mtcars$mpg))
sdlmm <- sd(log(mtcars$mpg))

vertical_line_intercepts <- c(
  mlmm - sdlmm,
  mlmm + sdlmm,
  mlmm - 2 * sdlmm,
  mlmm + 2 * sdlmm,
  mlmm - 3 * sdlmm,
  mlmm + 3 * sdlmm

vertical_line_intercepts_exp <- exp(vertical_line_intercepts)

verts <- data.frame(
  x = vertical_line_intercepts  %>% round(digits=2),
  text = vertical_line_intercepts_exp   %>% round(digits=2)
verts$y <- .7 # move up and down
mtcars %>% ggplot(aes(x = log(mpg))) + geom_density() +
  geom_vline(xintercept = vertical_line_intercepts) +
            label=text,y=y),nudge_x = .07)

1 Like

Wow this is great! Thank you very much for showing me how to do this