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_colbar title property adjustment #37

Closed
Breeze-Hu opened this issue Dec 16, 2024 · 4 comments
Closed

guide_colbar title property adjustment #37

Breeze-Hu opened this issue Dec 16, 2024 · 4 comments
Labels
🗑️ wontfix This will not be worked on

Comments

@Breeze-Hu
Copy link

Dear Teun van den Brand,

Can guide_colbar add title.position and title.hjust parameters to adjust the title position of the colorbar like guide_colorbar in ggplot?

The following code adjusts the position of the colorbar, the position of the colorbar header (top and hjust), but does not set the out-of-range triangle shape:

library(tidyverse)
library(legendry)
library(terra)

m <- matrix(1:25, nrow=5, ncol=5)
rm <- rast(m)

# from a "xyz" data.frame
data <- as.data.frame(rm, xy=TRUE)
bwr_colors <- c("blue", "white", "red")

ggplot(data = data,aes(x = x,y = y,fill = lyr.1)) +
  geom_tile() + 
  scale_fill_gradientn(
    colors = bwr_colors, 
    na.value = NA,
    limits = c(5, 20), oob = scales::oob_squish,
    guide = guide_colorbar(#shape = "triangle",
                         title.position = "top",
                         title.hjust = 0.5)) +
  theme_bw()+
  theme(
    legend.position = "bottom",             
    legend.key.width = unit(2, "cm"),       
    legend.key.height = unit(0.4, "cm"))

image

It's more elegant to use guide_colbar when setting the triangle shape, but it doesn't seem to be possible to set the title position of the colorbar:

ggplot(data = data,aes(x = x,y = y,fill = lyr.1)) +
  geom_tile() + 
  scale_fill_gradientn(
    colors = bwr_colors, 
    na.value = NA,
    limits = c(5, 20), oob = scales::oob_squish,
    guide = guide_colbar(shape = "triangle")
                         # title.position = "top",
                         # title.hjust = 0.5)
  ) +
  theme_bw()+
  theme(
    legend.position = "bottom",             
    legend.key.width = unit(2, "cm"),       
    legend.key.height = unit(0.4, "cm"))     
)

image

If settings similar to the above are made, an error is prompted:

`Error in guide_colbar(shape = "triangle", title.position = "top", title.hjust = 0.5) : 
  unused arguments (title.position = "top", title.hjust = 0.5)'

Best regards
Hu

@teunbrand
Copy link
Owner

Hi Hu, thank you for the report!

At the risk of sounding slightly rude: no, absolutely not.

The reason is that ggplot2 has removed these arguments and they only still work because of a backward compatibility mechanism. The legendry package is not beholden to ggplot2's backward compatibility, so it will not use the old deprecated arguments.

The "proper" way of setting such customisations is via the guide_colourbar(theme) argument (see blog), and this works the same way in legendry. You could use theme_guide() to be slightly more terse. I could add an example of this, but it really isn't any different than ggplot2.

library(tidyverse)
library(legendry)
library(terra)

m <- matrix(1:25, nrow=5, ncol=5)
rm <- rast(m)

# from a "xyz" data.frame
data <- as.data.frame(rm, xy=TRUE)
bwr_colors <- c("blue", "white", "red")

ggplot(data = data,aes(x = x,y = y,fill = lyr.1)) +
  geom_tile() + 
  scale_fill_gradientn(
    colors = bwr_colors, 
    na.value = NA,
    limits = c(5, 20), oob = scales::oob_squish,
    guide = guide_colbar(
      theme = theme_guide(
        title.position = "top",
        title = element_text(hjust = 0.5)
      )
    )) +
  theme_bw()+
  theme(
    legend.position = "bottom",             
    legend.key.width = unit(2, "cm"),       
    legend.key.height = unit(0.4, "cm")
  )

Created on 2024-12-16 with reprex v2.1.1

@teunbrand teunbrand added the 🗑️ wontfix This will not be worked on label Dec 16, 2024
@Breeze-Hu
Copy link
Author

Perfect! Thank you for your detailed reply. I read what you mentioned carefully and this does work well to unify the various attribute contents of the guide using the theme framework!
Thanks for contributing to the ggplot ecosystem! Packages such as legendry and ggh4x make plotting much more accessible and convenient!

@Breeze-Hu
Copy link
Author

Breeze-Hu commented Dec 18, 2024

Sorry to bother you again.
is there something problem with the height and width adjustment of the colorbar in the theme_guide function provided by legendry?
Using key.width and key.height seems to have no effect. But it is valid to use theme instead of theme_guide .

See details:
stackoverflow-question

@teunbrand
Copy link
Owner

You're right this seems like a bug. I'm tracking it in #41.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗑️ wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants