Align the legend and the title in the center of plot in r

Viewed 54

I have a dataset and I want to plot the heat map for them. my problem is: I want a vertical legend and also I want to put the title of legend in the middle of legend.

My data is a text file with is like the attached figure.

my data

Here is the code that I am using( when I change the horizontal to the vertical, the legend moves up and also the title is not in the center of legend).

# Heatmap
ggplot(heat, aes(Samples, Gene, fill= log(Folded_Change_of_Expression))) +
  geom_tile()+
  theme(axis.text.x = element_text(angle = 90))+ theme(axis.text.y = 
 element_text(size = 12)) +
  coord_fixed(ratio = 4)+theme(panel.background = element_blank()) +
  theme(axis.title = element_text(size = 16)) +
   guides(fill = guide_colourbar(barwidth = 5, barheight = 1, title = "log(Folded 
  Change of Expression)",
                            title.position = "top", direction = "horizontal"))

However, the output figure for me is not the thing that I want. Could you please help me with that? Thank youenter image description here

Legend that I want is same as this:

enter image description here

2 Answers

Here is one suggestion: New added last 3 lines remove existent guides lines:

library(tidyverse)

ggplot(heat, aes(Samples, Gene, fill= log(Folded_Change_of_Expression))) +
  geom_tile()+
  theme(axis.text.x = element_text(angle = 90))+
  theme(axis.text.y = element_text(size = 12)) +
  coord_fixed(ratio = 4)+
  theme(panel.background = element_blank()) +
  theme(axis.title = element_text(size = 16)) +
  scale_fill_continuous(guide = "colourbar") +
  theme(legend.title = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) +
  guides(fill = guide_legend(title.position = "right",
                             title = "log(Folded Change \n of Expression)"))

enter image description here

Perhaps the only way to 'center' the colourbar and title is for the colourbar to be larger than the legend title, e.g.

library(tidyverse)

heat <- data.frame(Samples = rep(c(paste("Control", 1:10, sep = "_"),
                               paste("Treat", 1:10, sep = "_")), each = 2),
                   Gene = c("Occludin", "Claudin"),
                   Folded_Change_of_Expression = runif(20, -4, 4))

ggplot(heat, aes(x = Samples, y = Gene, fill = log(abs(Folded_Change_of_Expression)))) +
  geom_tile() +
  coord_fixed(ratio = 4) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text.y = element_text(size = 12),
        panel.background = element_blank(),
        axis.title = element_text(size = 16),
        legend.direction = "vertical",
        legend.box = "vertical",
        legend.title = element_text(angle = 90,
                                    hjust = 0.5,
                                    vjust = 0.5),
        legend.justification = c(0.5, 0.5)) +
  guides(fill = guide_colourbar(title = "log(Fold Change in Gene Expression)",
                                title.position = "right",
                                title.hjust = 0.5,
                                title.vjust = 0.5,
                                legend.title.align = 0.5,
                                barheight = 14))

When the legend title is longer than the colourbar it appears to 'fall back' on a default justification. I've had a look at the source code for guide_colourbar (https://rdrr.io/github/SahaRahul/ggplot2/src/R/guide-colorbar.r) but it's not clear to me why this is the case. If you make the legend title smaller, it is positioned as expected:

library(tidyverse)

heat <- data.frame(Samples = rep(c(paste("Control", 1:10, sep = "_"),
                               paste("Treat", 1:10, sep = "_")), each = 2),
                   Gene = c("Occludin", "Claudin"),
                   Folded_Change_of_Expression = runif(20, -4, 4))

ggplot(heat, aes(x = Samples, y = Gene, fill = log(abs(Folded_Change_of_Expression)))) +
  geom_tile() +
  coord_fixed(ratio = 4) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text.y = element_text(size = 12),
        panel.background = element_blank(),
        axis.title = element_text(size = 16),
        legend.direction = "vertical",
        legend.box = "vertical",
        legend.title = element_text(angle = 90,
                                    hjust = 0,
                                    vjust = 0,
                                    size = 8),
        legend.justification = c(0, 0.5)) +
  guides(fill = guide_colourbar(title = "log(Fold Change in Gene Expression)",
                                title.position = "right",
                                title.vjust = 0.5,
                                barheight = 10))

Created on 2022-06-30 by the reprex package (v2.0.1)

Related