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

guide_colourbar_custom can't handle ticks with 2 elements #9

Closed
davidhodge931 opened this issue Apr 27, 2024 · 9 comments
Closed

guide_colourbar_custom can't handle ticks with 2 elements #9

davidhodge931 opened this issue Apr 27, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@davidhodge931
Copy link

library(gguidance)
#> Loading required package: ggplot2
library(tidyverse)
library(palmerpenguins)

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm,
        y = species,
        colour = flipper_length_mm)) +
  guides(
    colour = guide_colourbar_custom(),
  ) +
  theme(legend.ticks.length = ggplot2::rel(c(0.15, 0)))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).
#> Warning in gizmo_barcap(): Unable to check the capabilities of the png device.
#> Error in n_breaks < 1 || is_blank(element) || length == 0: 'length = 2' in coercion to 'logical(1)'

Created on 2024-04-27 with reprex v2.1.0

@teunbrand
Copy link
Owner

Thanks for the report David, you can see why I've labelled the package as experimental 😅
Should be a straightforward fix.

@teunbrand teunbrand added the bug Something isn't working label Apr 27, 2024
@davidhodge931
Copy link
Author

Thanks for the fixes!

It's all weirder edge-case stuff anyway, but might as well fix if it's not too hard

@teunbrand
Copy link
Owner

Should no longer throw any errors:

library(gguidance)
#> Loading required package: ggplot2
library(tidyverse)
library(palmerpenguins)

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm,
        y = species,
        colour = flipper_length_mm)) +
  guides(
    colour = guide_colourbar_custom(),
  ) +
  theme(legend.ticks.length = ggplot2::rel(c(0.15, 0)))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Note that implementation differs from ggplot2, because of the 'little guides in a trenchcoat' approach.
To mirror ggplot2's approach to disabling ticks on one side, you can do the following:

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm,
        y = species,
        colour = flipper_length_mm)) +
  guides(
    colour = guide_colourbar_custom(second_guide = "none"),
  )
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-04-27 with reprex v2.1.0

@davidhodge931
Copy link
Author

Awesome, thanks!

Minor thing - if your theme that you've added already has 2 elements in it, then there is a tick only every 2nd break even once you add second_guide = "none"

library(gguidance)
library(tidyverse)
library(palmerpenguins)

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm,
    y = species,
    col = flipper_length_mm),
  ) +
  scale_colour_gradientn(colours = blues9, limits = c(NA, 220)) +
  theme(legend.ticks.length = rel(c(0, 0.5))) +
  guides(colour = guide_colourbar_custom(second_guide = "none"))

image

@davidhodge931
Copy link
Author

In addition to above, are you planning on adding second_guide functionality to guide_colourbar_custom and guide_colour_ring?

@teunbrand
Copy link
Owner

then there is a tick only every 2nd break

Yes but this is also what happens when you use e.g. theme(axis.ticks.length.x.bottom = rel(c(0, 1))), so I'd say this is the typical outcome of specifying tick lengths.

In addition to above, are you planning on adding second_guide functionality to guide_colourbar_custom and guide_colour_ring?

No they already have first_guide and second_guide arguments. It is dependent on the legend.text.position where the first and where the second is drawn. The colour ring has inner_guide and outer_guide arguments that have the same role, but are not dependent on the theme (as legend.text.position = "outside" is not a valid option).

@davidhodge931
Copy link
Author

Re the 2nd break thing, it works differently to ggplot2::guide_colourbar which keeps all ticks in that situation.

Re the first_guide/second guide, whoops missed that

@teunbrand
Copy link
Owner

it works differently to ggplot2::guide_colourbar which keeps all ticks in that situation.

True, but I'm not going to mirror that guide perfectly as I'd like to be free of the historic bagage of that guide so that I can do it the way I think it is best. Primarily, it is important to me that the tickmarks and labels are individual guides that can be tweaked as one sees fit separately from the colourbar itself. In the process, we might lose nuances like that, but it is still very much possible to achieve the same result.

@davidhodge931
Copy link
Author

Fair enough - sounds like a good approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants