Skip to content

Instantly share code, notes, and snippets.

@James-Ansley
Last active February 3, 2024 10:08
Show Gist options
  • Save James-Ansley/7fa33bd17696141ad1e0026e16f4e473 to your computer and use it in GitHub Desktop.
Save James-Ansley/7fa33bd17696141ad1e0026e16f4e473 to your computer and use it in GitHub Desktop.
A simple density plot generator

A function to generate simple density plots.

Example

Single density plots can be generated:

(
  density_plot(
      test_1_data,
      x = "grade",
      title = "Density of Grades",
      limits = c(0, 100)
  )
  + labs(x = "Grade (/100)")
)

image

Or plots can be wrapped by proviging a group_on argument:

(
  density_plot(
      test_1_data,
      x = "grade",
      title = "Density of Grades By Stream",
      group_on = "stream",
      limits = c(0, 100)
  )
  + labs(x = "Grade (/100)")
)

image

library(tidyverse)
#' Returns a beautiful density plot
#'
#' @param data A data frame
#' @param x The x-axis variable as a string
#' @param group_on (optional) The variable to facet-wrap on as a string
#' @param title (optional) The title, if given will append "(n=<n>)"
#' @param limits (optional) a numeric vector of length 2 specifying the min and max values
#' @returns A ggplot2 plot
density_plot <- function(
data,
x,
title = NULL,
group_on = NULL,
limits = NULL
) {
plot <- ggplot(data, aes(x = !!as.name(x)))
if (!is.null(group_on)) {
data <- data |> group_by(!!as.name(group_on))
}
summary <- (
data
|> summarise(
mean = mean(!!as.name(x)),
median = median(!!as.name(x)),
n = n()
)
)
if (!is.null(group_on)) {
plot <- plot + facet_wrap(
~ get(group_on),
dir = "v",
labeller = as_labeller(~ paste0(
.x,
" (n=",
(summary |> filter(!!as.name(group_on) == .x) |> pull(n)),
")"
))
)
}
if (!is.null(title)) {
total <- summary |> pull(n) |> sum()
plot <- plot + labs(title = paste0(title, " (n=", total, ")"))
}
return(
plot
+ geom_density(alpha = 0.25, fill = "salmon")
+ theme(plot.title = element_text(face = "bold"))
+ scale_x_continuous(expand = expansion(add = 1.05), limits = limits)
+ scale_colour_manual(
name = "Summary", values = c(mean = "red", median = "blue")
)
+ scale_linetype_manual(
name = "Summary", values = c(mean = "solid", median = "longdash")
)
+ geom_vline(
aes(xintercept = mean, color = "mean", linetype = "mean"),
data = summary
)
+ geom_vline(
aes(xintercept = median, color = "median", linetype = "median"),
data = summary
)
+ labs(color = "Summary", linetype = "Summary")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment