I'm creating a visualization of missing data by slightly tweaking some of the code from the missmap function in the Amelia package. I want to draw borders around my rectangles, but I can't figure out a way to do that in ggplot2.
I found the function "borders()" but that appears to be related to map work. I also tried using geom_rect, but it seems like that would require me to specify mins and maxes. Geom_raster seems to be doing exactly what I need, but I can't figure out how to specify borders.
This example code creates the visualization that I'm imagining, but I have more variables in the "real" version and I'd like to be able to outline each variable (var1, var2, etc.) with a line (border).
#Dataset
missmap_data_test <- data.frame(var1 = c(11, 26, NA, NA, 15),
var2 = c(NA, NA, 0, NA, 1))
#Create Function
ggplot_missing <-
function(x){
x %>%
is.na %>%
melt %>%
ggplot(data = .,
aes(x = Var2,
y = Var1)) +
geom_raster(aes(fill = value)) +
scale_fill_grey(name = "",
labels = c("Present","Missing")) +
theme_minimal() +
theme(axis.text.x = element_text(angle=90, hjust=1)) +
labs(x = "Variables in Dataset",
y = "Observations")
}
#Feed the function my new data
ggplot_missing(missmap_data_test)