{ggblanket}: common x and y scale adjustments
I was messing around with some scale adjustments with my {ggblanket} package, and thought I’d share this as a post.
It’s by no means a complete demonstration of the functionality available.
But it demonstrates a bit of how x and y scales can be adjusted.. as well as some other stuff randomly thrown in.
library(palmerpenguins) library(tidyverse) library(lubridate) library(ggblanket)
Note x scale default limits of min/max breaks and zero expanding
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy)
Make the theme horizontal lines only
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, theme = gg_theme(y_grid = TRUE))
Adjust limits of an x date variable to the min and max of the data
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_limits = c(NA_Date_, NA_Date_))
Add expand of 0.05
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_limits = c(NA_Date_, NA_Date_), x_expand = c(0.05, 0.05))
Make more breaks using the pretty algorithm with the x_breaks_n argument.
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_breaks_n = 10)
Make breaks using the scales::fullseq algorithm using the x_breaks_width argument.
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_breaks_width = "3 years")
Specify your own specific breaks. In this case, we are using the seq function and working backwards to ensure our last break is the maximum of the data. Note that if your breaks do not cover all of the data, you will need to specify limits of c(NA, NA)
or c(NA_Data_, NA_Data_)
to include all of the data.
x_vctr <- economics %>% slice_head(n = 100) %>% pull(date) x_breaks <- seq(from = max(x_vctr), to = min(x_vctr), by = "-19 months") economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_breaks = x_breaks, x_limits = c(NA_Date_, NA_Date_))
Use ggplot2’s default breaks, limits, labels and expand
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy) + scale_x_date()
Convert to an area graph
economics %>% slice_head(n = 100) %>% gg_area(x = date, y = unemploy)
Convert to a line graph
economics %>% slice_head(n = 100) %>% gg_line(x = date, y = unemploy, size = 1)
Add zero in the y scale
economics %>% slice_head(n = 100) %>% gg_line(x = date, y = unemploy, size = 1, y_zero = TRUE)
Convert to a step graph. And colour by the y variable. Note no legend is provided, as it is the values can be read off the y scale.
economics %>% slice_head(n = 100) %>% gg_step(x = date, y = unemploy, col = unemploy)
Convert to a ribbon to show uncertainty
economics %>% slice_head(n = 100) %>% gg_ribbon(date, y = unemploy, ymin = unemploy - 500, ymax = unemploy + 500, y_zero = TRUE) + geom_line()
Remove the outer lines
economics %>% slice_head(n = 100) %>% gg_ribbon(x = date, y = unemploy, ymin = unemploy - 500, ymax = unemploy + 500, y_zero = TRUE, pal = scales::alpha(pal_viridis_mix(1), 0)) + geom_line(col = scales::alpha(pal_viridis_mix(1), 1))
Change the y title
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, y_title = "Unemployment")
Remove the x title
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy, x_title = "")
Alternative way to remove the x title.
economics %>% slice_head(n = 100) %>% gg_col(x = date, y = unemploy) + ggplot2::labs(x = NULL)
For further information, see the ggblanket website.