Home > OS >  Putting a border around a column on ggheatmap
Putting a border around a column on ggheatmap

Time:11-05

I have been trying to put a border around a specific column of a correlation heatmap I made using ggheatmap. I want to place a border around the correlation values for the 'quality' or 7th element, but can't seem to get it to work properly. I have a similar heatmap to the one on this page My current heatmap

CodePudding user response:

We don't have your data or code, but we can make a similar plot using the built in mtcars data set:

library(ggplot2)

cormat <- cor(mtcars)
cormat[upper.tri(cormat)] <- NA

heatmap <- ggplot(reshape2::melt(cormat),
       aes(Var1, Var2, fill = value))  
  geom_tile()  
  geom_text(aes(label = round(value, 2)), na.rm = TRUE)  
  scale_fill_gradientn(colours = c('blue', 'white', 'red'), 
                       na.value = 'white')  
  labs(x = NULL, y = NULL)  
  theme_minimal(base_size = 16)  
  theme(legend.position = c(0.4, 0.9),
        legend.direction = 'horizontal')  
  coord_equal()

heatmap

enter image description here

To draw the rectangle, we can add it using the ggplot function annotate

heatmap   
  annotate('rect', xmin = 0.5, xmax = 3.5, ymin = 0.5, ymax = 3.5,
           fill = NA, color = 'black', size = 1)

enter image description here

  • Related