if (FALSE) {
library(ggplot2)
library(dplyr)
library(brms)
library(modelr)
theme_set(theme_light())
m_mpg = brm(mpg ~ hp * cyl, data = mtcars)
step = 1
mtcars %>%
group_by(cyl) %>%
data_grid(hp = seq_range(hp, by = step)) %>%
add_predicted_draws(m_mpg) %>%
summarise(density_bins(.prediction), .groups = "drop") %>%
ggplot() +
geom_rect(aes(
xmin = hp - step/2, ymin = lower, ymax = upper, xmax = hp + step/2,
fill = ordered(cyl), alpha = density
)) +
geom_point(aes(x = hp, y = mpg, fill = ordered(cyl)), shape = 21, data = mtcars) +
scale_alpha_continuous(range = c(0, 1)) +
scale_fill_brewer(palette = "Set2")
}
Run the code above in your browser using DataLab