An X, Y, and Z dataset was plotted using geom_tile
. I want to make a contour line around the tiles with values less than or equal to 2. For this, I used the stat_contour
function, but the result was not as expected. How can I get the expected result? (last image)
library(ggplot2)
X <- 1:3
Y <- seq(0,20,10)
df <- expand.grid(X = X, Y = Y)
df$Z <- c(5,4,9,2.1,1.5,1.2,6,7,1.9)
ggplot(df, aes(X, Y)) +
geom_tile(aes(fill = Z)) +
scale_fill_distiller(palette = "RdYlGn") +
stat_contour(aes(z = Z),
breaks = 2,
color = 1)
I want something like: