rggplot2plotlegendcolorbar

How can I make the legend color bar the same height as my plot panel?


I have generated a simple plot in R that shows the correlation coefficients for a set of data. Currently, the legend colorbar on the right side of the plot is a fraction of the entire plot size.

I would like the legend colorbar to be same height as the plot. I thought that I could use the legend.key.height to do this, but I have found that is not the case. I investigated the grid package unit function and found that there were some normalized units in there but when I tried them (unit(1, "npc")), the colorbar was way too tall and went off the page.

How can I make the legend the same height as the plot itself?

A full self contained example is below:

library(ggplot2)

corrs <- structure(list(Var1 = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), levels = c("Var1", "Var2", "Var3"), class = "factor"), Var2 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), levels = c("Var1", "Var2", "Var3"), class = "factor"), value = c(1, -0.11814395012334, -0.91732952510938, -0.969618394505233, 1, -0.00122085912153125, -0.191116513684392, -0.0373711776919663, 1)), class = "data.frame", row.names = c(NA, -9L))

ggplot(corrs, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  theme(
    panel.border = element_blank(),
    axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
    aspect.ratio = 1,
    legend.position = "right",
    legend.key.height = unit(1, "inch")
  )

Created on 2022-12-29 with reprex v2.0.2


Solution

  • It seems quite tricky, the closest I got was this,

    ## panel height is 1null, so we work it out by subtracting the other heights from 1npc
    ## and 1line for the default plot margins
    
    panel_height <- unit(1,"npc") - sum(ggplotGrob(plot)[["heights"]][-3]) - unit(1,"line")
    plot + guides(fill= guide_colorbar(barheight=panel_height))
    

    unfortunately the vertical justification is a bit off.