ggplot2
ggplot2 copied to clipboard
Natively Cap the Axis Lines, as featured on the lemon package (feature)
---- Feature Request: Natively Cap the Axis Lines ----
It would be amazing to have the option to cap the axis lines, such as featured on the lemon package. The lemon package, however, requires to remove the ggplot2 default axis lines so that new ones can be drawn:
In order to manipulate the axis lines, they must be drawn. Modify the theme so the panel.border is not drawn (it will be on top of the axis lines), and have the axis lines drawn.
Therefore, it would be nice being able to cap the axis lines natively (without getting rid of the original axis lines).
Please find below a reprex of an example chart with capped axis lines:
# one can install the `lemon` package from CRAN.
# install.packages('lemon')
library(tidyverse)
library(lemon)
set.seed(2)
letters_data <- purrr::map_dfr(
letters,
~ data.frame(
idx = 1:400,
value = cumsum(runif(400, -1, 1)),
type = .,
flag = sample(c(TRUE, FALSE), size = 400, replace = TRUE),
stringsAsFactors = FALSE
)
)
letters_data %>%
ggplot() +
geom_line(aes(idx, value, colour = type)) +
coord_capped_cart(bottom = "both",
left = "both",
ylim = c(-50,25)) +
theme_classic() +
theme(legend.position="none",
axis.ticks.length = unit(.25, "cm"),
axis.line = element_line()) +
coord_capped_cart(bottom=capped_horizontal(),
xlim = c(0, 400),
ylim = c(-40, 20),
left=capped_vertical(capped = "both"))

I'll be sounding like a broken record, but once guides have been rewritten we will certainly look into something like this
I've written something similar before, so it wouldn't be much of a bother to slap onto #4879 if the ggplot team agrees.
absolutely - I've been wanting to make something like this forever. I just think it makes most sense after you've rewritten the guide system 😉
So, for the difficult bit: what should be the name of this axis guide?
Here is an incomplete list of suggestions:
guide_axis_float()guide_axis_hover()guide_axis_cap()/guide_axis_capped()guide_axis_trunc()/guide_axis_truncated()guide_axis_resect()guide_axis_short()
Does it make sense to introduce an entire separate axis guide for this purpose? Isn't the only difference to the regular guides that the axis line gets truncated? If I'm seeing this correctly, I'd suggest to implement only one guide function and make the axis line truncation just an argument of that guide.
That would work too. Also in that case, what should the argument name be?
What values should it accept? A simple truncate = TRUE/FALSE to let the line start and end at the most extreme ticks? Or something more flexible, like functions that operate on {breaks/limits} or could we supply data-values directly (or both)?
I would suggest to implement it first and then think about this. It's not immediately clear to me that anything other than TRUE/FALSE is needed, but sometimes alternative options reveal themselves once you actually have working code.