I have the following heatmap generated in R using geom_tile in the ggplot2 package.
# Dummy data
x <- LETTERS[1:5]
y <- paste0("var", seq(1,5))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(25, 0, 5)
# Heatmap
ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
scale_fill_gradient(low = "green", high = "red")

Is there any way to draw a border around specific tiles only? For example, I would like to have a border around the cell in the top left hand corner (A:var5) and the cell in the centre (C:var3).
You could do:
ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
geom_tile(data = data[c(13, 21),], fill = NA, color = "black", size = 2) +
scale_fill_gradient(low = "green", high = "red")

Here are two methods, depending on your needs:
geom_tile() for that subset, with different aesthetics:p <- ggplot(data, aes(X, Y, fill= Z)) +
geom_tile() +
scale_fill_gradient(low = "green", high = "red")
d <- data.frame(X=c("A","C"), Y=c("var5", "var3"))
dat <- merge(data, d)
p + geom_tile(data=dat, aes(X,Y), fill="transparent", colour="black", size=1)
p + annotate("rect", xmin=c(0.5, 2.5), xmax=c(1.5, 3.5), ymin=c(4.5,2.5), ymax=c(5.5,3.5), colour="black", fill="transparent", size=1)
Results are equivalent:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With