Skip to content

Issues with label control when combining scale_fill_discrete with guide_colorsteps #6785

@Breeze-Hu

Description

@Breeze-Hu

Hi! Happy Merry Christmas in Advance!

I defined a function to alternate the labels in the legend.
However, whether using single or double intervals, the last label (as shown as 0.4 in the figure) always appears. When running the function independently, it produces puzzling results. The generated labels do not match those in the figure.

library(ggplot2)

v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))

v +
  geom_contour_filled(breaks = seq(0, 0.04, length.out = 8)) +
  scale_fill_discrete(
    palette = scales::pal_brewer(palette = "Blues"),
    labels = function(brks) {
      brks[seq_along(brks) %% 2 != 0] <- ""
      return(brks)
    } 
  ) +
  guides(fill = guide_colorsteps(show.limits = TRUE, position = 'bottom')) +
  theme(legend.key.width = unit(40, 'pt'))

Image
a  <- function(brks) {
      brks[seq_along(brks) %% 2 != 0] <- ""
      return(brks)
    }

a(seq(0, 0.04, length.out = 8))

#[1] ""                    "0.00571428571428571" ""                   
#[4] "0.0171428571428571"  ""                    "0.0285714285714286" 
#[7] ""                    "0.04"           

Best wishes,
Hu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions