scales icon indicating copy to clipboard operation
scales copied to clipboard

Feature request: label_2nd function to label every second break

Open davidhodge931 opened this issue 3 years ago • 1 comments

Credit to Simon Woodwood for this function https://community.rstudio.com/t/how-to-automatically-skip-some-x-labels/65702/2

library(palmerpenguins)
library(tidyverse)

label_second <- function(x){
  x[seq(2, length(x), 2)] <- ""
  x
}

penguins %>% 
  ggplot() +
  geom_jitter(
    aes(
      x = sex, 
      y = body_mass_g, 
    )) +
    scale_y_continuous(breaks = scales::breaks_pretty(10), labels = label_2nd)

image

Thought label_2nd rather than label_second, as then users know that this does not relate to time

davidhodge931 avatar Sep 13 '22 04:09 davidhodge931

This would be particularly useful when facetting

davidhodge931 avatar Sep 13 '22 05:09 davidhodge931

label_nth <- function(x, nth = 1) replace(x, seq_along(x)%%nth!=0, "")

davidhodge931 avatar Sep 09 '23 22:09 davidhodge931

I think this would work better in stringr

davidhodge931 avatar Sep 11 '23 02:09 davidhodge931