Skip to content

Instantly share code, notes, and snippets.

@mgrabovsky
Last active April 9, 2024 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgrabovsky/d6c15a14c6347e16ce34ee1245fbac35 to your computer and use it in GitHub Desktop.
Save mgrabovsky/d6c15a14c6347e16ce34ee1245fbac35 to your computer and use it in GitHub Desktop.
Analysing my tips in cafés
library(tidyverse)
# Set Monday as the first day of the week.
options(lubridate.week.start = 1)
theme_set(
theme_minimal() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank()
)
)
tips <- read_csv("tips.csv") |>
mutate(
ratio = tip / bill,
date = dmy(date),
wday = wday(date, label = TRUE, abbr = TRUE),
week = week(date),
year = year(date)
)
scale_y_percent <- function() {
scale_y_continuous(labels = scales::label_percent(10), limits = c(0, .4))
}
# First, an elementary histogram of tip values in percent of bill total.
ggplot(tips, aes(ratio)) +
geom_histogram(fill = "steelblue", binwidth = .05, boundary = 0, colour = "white") +
scale_x_continuous(labels = scales::label_percent(10))
# Scatter plot of bill total vs. tip %.
ggplot(tips, aes(bill, ratio)) +
geom_point(colour = "dodgerblue4", shape = 16, alpha = .5) +
scale_y_percent()
# Time series of number of recorded tips per week.
ggplot(tips, aes(week)) +
geom_bar(fill = "steelblue", width = 1) +
facet_grid(rows = vars(year))
# Relative values of all tips each week as points.
ggplot(tips, aes(week, ratio)) +
geom_point(colour = "dodgerblue4", shape = 16, alpha = .5) +
geom_hline(yintercept = 0) +
scale_y_percent() +
facet_grid(rows = vars(year))
# Violin plot of relative values by day of week with overlaid jittered observations.
ggplot(tips, aes(wday, ratio)) +
geom_violin(colour = "grey", draw_quantiles = .5) +
geom_jitter(colour = "dodgerblue4", shape = 16, alpha = .5, width = .2) +
scale_y_percent()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment