library(tidyverse)
library(patchwork) # To add plots side-by-side!
sim_df <- data.frame(Y = rbeta(10000, 1, 2)) %>%
mutate(U = 2*Y-1)
ggplot(data = sim_df) +
geom_histogram(aes(x = Y,y=after_stat(density)),
fill = 'goldenrod',
color ='black',
center = 0.02, binwidth = 0.04)+
geom_function(fun = \(y) dbeta(y, 1, 2),
linewidth = 1,
xlim = c(0,1)) +
xlim(c(-1,1)) + ylim(c(0,2.5)) +
labs(x = 'y', y = expression(f[Y](y))) +
theme_classic(base_size = 14) +
ggplot(data = sim_df) +
geom_histogram(aes(x = U,y=after_stat(density)),
fill = 'cornflowerblue',
color ='black',
center = -0.98, binwidth = 0.04)+
geom_function(fun = \(u) (1-u)/2,
linewidth = 1) +
xlim(c(-1,1)) + ylim(c(0,2.5)) +
labs(x = 'u', y = expression(f[U](u))) +
theme_classic(base_size = 14)