Last active
September 29, 2022 17:01
-
-
Save MonkmanMH/201c3ffe4aa0dfa0a76ebb265bdd1745 to your computer and use it in GitHub Desktop.
Y axis breaks at min & max value {ggplot2}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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