Ppd_dens_overlay() - it there a way to scale y axis values between 0 and 1?

geom_density() allows the user to scale y axis values between 0 and 1 (r - geom_density y-axis goes above 1 - Stack Overflow). I was wondering if it’s possible to do the same in ppd_dens_overlay()

library(ggplot2)
color_scheme_set("brightblue")
preds <- example_yrep_draws()
ppd_dens_overlay(ypred = preds[1:50, ])+theme_bw()

Did you mean ppc_dens_overlay? Even after making that change, I’m still getting an error when running the example code: Error in ppc_data(y, yrep) : argument "y" is missing, with no default In addition: Warning message: The following arguments were unrecognized and ignored: ypred.

Here’s an example of rescaling the y-axis values of a ppd_dens_overlay plot. We use rescale from the scales package to take care of the rescaling. Note that the code below just rescales the axis and sets new axis breaks that are round values on the new scale.

library(bayesplot)
library(tidyverse)
theme_set(theme_bw())
library(scales)
library(patchwork) # For plot layouts

color_scheme_set("brightblue")
preds <- example_yrep_draws()

p1 = ppd_dens_overlay(ypred = preds[1:50, ]) + theme_bw()

# Get actual y-range of plot
r = layer_scales(p1)$y$range$range

# Set breaks on zero-one scale
brks = seq(0,1,0.2)

# Apply breaks on original scale
p2 = p1 +
  scale_y_continuous(breaks=rescale(brks, to=r),
                     labels=brks)

p1 / p2 + plot_layout(guides="collect")

If you want to rescale the density values so that all the individual yreps are scaled to zero-one, you can do this:

d = p1$data
d
#> # A tibble: 21,700 × 5
#>     y_id y_name rep_id rep_label              value
#>    <int> <fct>   <int> <fct>                  <dbl>
#>  1     1 1           1 italic(y)[pred] ( 1 )   74.3
#>  2     1 1           2 italic(y)[pred] ( 2 )  109. 
#>  3     1 1           3 italic(y)[pred] ( 3 )  121. 
#>  4     1 1           4 italic(y)[pred] ( 4 )   95.6
#>  5     1 1           5 italic(y)[pred] ( 5 )   83.9
#>  6     1 1           6 italic(y)[pred] ( 6 )  101. 
#>  7     1 1           7 italic(y)[pred] ( 7 )   99.9
#>  8     1 1           8 italic(y)[pred] ( 8 )  101. 
#>  9     1 1           9 italic(y)[pred] ( 9 )  102. 
#> 10     1 1          10 italic(y)[pred] ( 10 )  69.9
#> # … with 21,690 more rows

# You can also get the data without creating the plot:
# d = ppd_data(preds[1:50,])

d %>% 
  ggplot(aes(value, ..scaled.., group=rep_label)) +
  geom_density(colour=color_scheme_get()[[3]], size=0.2) 

1 Like

ppd_dens_overlay() is part of the most recent version of bayesplot. Maybe you haven’t upgraded yet?

Yes, thanks for the clarification. I’ve updated bayesplot and edited my answer above. Let me know if that’s what you had in mind.

1 Like