Skip to content

guide_colbar title property adjustment #37

@Breeze-Hu

Description

@Breeze-Hu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions