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