Search code examples
rggplot2

legendry and ggplot theme_guide Adjustment of height and width failure


It is valid to use the theme to adjust the legend.key.height height and legend.key.width width of the legend at the end.

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")
  )

enter image description here

But trying to write it to theme_guide doesn't seem to work.

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(position = 'bottom',
      theme = theme_guide(key.width = unit(2, "cm"),       
                          key.height = unit(0.4,'cm'),
                          title.position = "top",
                          title = element_text(hjust = 0.5)))) +
  theme_bw()

enter image description here

Is this a bug or is it because of my code?


Solution

  • This issue has been fixed by the developer! See details: Github

    ————————————————————————

    One solution I've found so far is to write it into guides so that the height and width can be adjusted.

    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,
      ) +
      guides(fill =  guide_colbar(
        position = 'bottom',
        theme = theme(
          legend.key.width  = unit(4, "cm"),
          legend.key.height = unit(0.5, "cm"),
          legend.title.position = "top",
          legend.title = element_text(hjust = 0.5)
        )
      ))+
    theme_bw()
    

    enter image description here

    Also, use theme = theme instead of theme = theme_guide:

     guide = guide_colbar(position = 'bottom',theme = theme(...)
    
    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(
          position = 'bottom',
          theme = theme(
            legend.key.width = unit(5, "cm"),
            legend.key.height = unit(0.4, 'cm'),
            title.position = "top",
            title = element_text(hjust = 0.5)
          )
        )
      ) +
      theme_bw()