Skip to content

Instantly share code, notes, and snippets.

@bwiernik
Last active March 4, 2020 05:17
Show Gist options
  • Save bwiernik/06187e3ef0d89aadc2128aa91b86f134 to your computer and use it in GitHub Desktop.
Save bwiernik/06187e3ef0d89aadc2128aa91b86f134 to your computer and use it in GitHub Desktop.
Ridge plot for plotting logistic regression data
library(dplyr)
library(ggplot2)
library(ggridges)
dat <- select(psych::bfi, age, education, gender) %>%
mutate(education = as.factor(education),
gender = as.factor(gender),
gender_edu = paste(education, gender)) %>%
na.omit()
ggplot(dat, aes(x = age,
y = gender_edu,
group = gender_edu,
color = education,
fill = education
)) +
geom_density_ridges(aes(alpha = gender)) +
scale_alpha_discrete(range = c(.2, .6)) +
geom_point(alpha = .3)
ggplot(dat, aes(x = age,
y = gender_edu,
group = gender_edu,
color = education,
fill = education
)) +
geom_density_ridges(aes(alpha = gender), scale = 1) + # Change scale to adjust amount of density overlap
scale_alpha_discrete(range = c(.2, .6)) +
geom_jitter(width = 0, height = .2, alpha = .3)
mod <- glm(gender ~ age + education, family = "binomial", data = dat)
newdat <- bind_cols(dat, pred = predict(mod, type = "response"))
# Scatterplot using predicted probabilities and point shape
ggplot(newdat, aes(x = age, y = pred)) +
geom_point(aes(alpha = gender, color = education)) +
geom_point(aes(color = education), shape = 1) +
scale_alpha_discrete(range = c(0, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment