Reputation: 169
My data looks like this:
Col1 Var1 Var2
A 1 NA
B NA 1
C 1 NA
D 1 1
I want to create a grid plot of missing data just as one can do using the Amelia package (https://www.r-bloggers.com/ggplot-your-missing-data-2/)
However, I find the result pretty ugly. Basically, I want variables in col 1 to be in the x axis and Var1 and Va2 on the Y axis. Like grey when present and black when absent. Does that make sense?
Any advice? I placed the Amelia plot below
Upvotes: 2
Views: 273
Reputation: 37641
A base R solution
Dat = t(matrix(as.numeric(is.na(df[,2:3])), nrow=nrow(df)))
rownames(Dat) = names(df)[2:3]
colnames(Dat) = df$Col1
heatmap(Dat, NA, NA, scale="none", col=c("gray", "black"))
Data
df = read.table(text="Col1 Var1 Var2
A 1 NA
B NA 1
C 1 NA
D 1 1",
header=TRUE)
Upvotes: 2
Reputation: 26343
Here is a ggplot2
option.
Reshape your data from wide to long first and replace ǸA
with 0
s (or any other value).
df1_long <- tidyr::gather(replace(df1, is.na(df1), 0), key, value, -Col1)
Now plot
library(ggplot)
ggplot(df1_long, aes(Col1, key, fill = factor(value))) +
geom_tile() +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
scale_fill_manual(values = c(`0` = "black",
`1` = "grey80"),
labels = c("Missing", "Observed")) +
labs(title = "Your Title",
fill = NULL,
x = NULL,
y = NULL) +
coord_equal() +
theme(legend.position = "bottom")
data
df1 <- structure(list(Col1 = c("A", "B", "C", "D"), Var1 = c(1L, NA,
1L, 1L), Var2 = c(NA, 1L, NA, 1L)), .Names = c("Col1", "Var1",
"Var2"), class = "data.frame", row.names = c(NA, -4L))
Upvotes: 1