Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Last active September 29, 2022 17:01
Show Gist options
  • Save MonkmanMH/201c3ffe4aa0dfa0a76ebb265bdd1745 to your computer and use it in GitHub Desktop.
Save MonkmanMH/201c3ffe4aa0dfa0a76ebb265bdd1745 to your computer and use it in GitHub Desktop.
Y axis breaks at min & max value {ggplot2}
# modified from / inspired by @GShotwell
# gist: https://gist.github.com/GShotwell/b19ef520b6d56f61a830fabb3454965b
df <- tibble(
value = rnorm(100, 50, 10),
date = seq.Date(from = ymd("2022-01-01"),
ymd("2022-04-10"),
by = "day"))
breaks_ts_min_max <- function(df, date_var, val) {
labs <- df |>
summarize(
breaks = c(
min({{val}}),
max({{val}}))
)
ggplot(df,
aes(x = {{date_var}},
y = {{val}})) +
geom_path() +
# scale_y_continuous(breaks = labs$breaks, minor_breaks = NULL) +
scale_y_continuous(breaks = labs$breaks) +
theme_minimal()
}
breaks_ts_min_max(df, date, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment