Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Natively Cap the Axis Lines, as featured on the lemon package (feature) #4907

Closed
asolisc opened this issue Jul 14, 2022 · 8 comments · Fixed by #5289
Closed

Natively Cap the Axis Lines, as featured on the lemon package (feature) #4907

asolisc opened this issue Jul 14, 2022 · 8 comments · Fixed by #5289
Labels
feature a feature request or enhancement guides 📏

Comments

@asolisc
Copy link

asolisc commented Jul 14, 2022

---- 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"))

image

@thomasp85
Copy link
Member

I'll be sounding like a broken record, but once guides have been rewritten we will certainly look into something like this

@teunbrand
Copy link
Collaborator

I've written something similar before, so it wouldn't be much of a bother to slap onto #4879 if the ggplot team agrees.

@thomasp85
Copy link
Member

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 😉

@teunbrand teunbrand added feature a feature request or enhancement guides 📏 labels Dec 23, 2022
@teunbrand
Copy link
Collaborator

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()

@clauswilke
Copy link
Member

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.

@teunbrand
Copy link
Collaborator

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)?

@clauswilke
Copy link
Member

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.

@eejd
Copy link

eejd commented Jul 30, 2024

I know this is merged, but I wanted to point out that the option to switch guides from complete to truncated should be supported as a theme element / option as well. I have been waiting for this feature and have used various workarounds, but I realized recently working on a large project where I needed to change a lot of aesthetics across a large number of plots, the ability to reformat as much as possible via theme application is very useful. I hope that the default guide could be complete, specified in the theme, with the option to truncate being available as a guide option or by changing the theme default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement guides 📏
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants