ggeasy icon indicating copy to clipboard operation
ggeasy copied to clipboard

easy_rotate_*_labels: consider updating the hjust and vjust be updated?

Open davidhodge931 opened this issue 1 year ago • 5 comments

Demo as how your functions rotate the text, compared to an alternative suggestion.

Would require breaking code to change, so not sure if you're up for that or not

library(tidyverse)
library(palmerpenguins)

penguins |> 
  ggplot() +
  geom_bar(aes(x = species, fill = island)) +
  ggeasy::easy_rotate_x_labels(teach = TRUE) 

penguins |> 
  ggplot() +
  geom_bar(aes(x = species, fill = island)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))  

penguins |> 
  mutate(species = ifelse(species == "Adelie", paste0(species, "\nspecies"), as.character(species))) |>
  ggplot() +
  geom_bar(aes(y = species, fill = island)) +
  ggeasy::easy_rotate_y_labels(teach = TRUE) 

penguins |> 
  mutate(species = ifelse(species == "Adelie", paste0(species, "\nspecies"), as.character(species))) |>
  ggplot() +
  geom_bar(aes(y = species, fill = island)) +
  theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0))  

davidhodge931 avatar Oct 30 '24 01:10 davidhodge931

The side argument in ggeasy::easy_rotate_x_labels(side = "right") already sets the hjust according to

left = 0
middle = 0.5
right = 1

and maybe that's not the best name for it, but there's no argument for the vjust in {ggeasy}.

I could add an additional vertical argument that adjusts vjust. Other suggestions?

jonocarroll avatar Oct 30 '24 01:10 jonocarroll

Ah, I don't think my way is as flexible as yours.. it works for rotating 90, but not for other angles

davidhodge931 avatar Oct 30 '24 02:10 davidhodge931

I think it's best to have minimal new concepts for people to learn

An idea is to have a couple of optimised shortcut 90 degree functions..

easy_rotate_x_labels_90 <- function() {
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))  
} 

easy_rotate_y_labels_90 <- function() {
  theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0))    
} 

davidhodge931 avatar Oct 30 '24 02:10 davidhodge931

Is the argument just that easy_rotate_*_labels() should have better defaults? I can expose the vjust with a more friendly argument, but otherwise angle=90 is the default.

jonocarroll avatar Oct 30 '24 03:10 jonocarroll

Yeah, I think the best defaults are labels centred on the tick, and justified closest to the axis. I.e. like in my example code above.

But I don't know how that can be done for all angles, other than if you make a standalone 90 function.

Feel free to close

davidhodge931 avatar Oct 30 '24 05:10 davidhodge931