I have this huge data frame (1558 obs of 2431 variables) filled with NA
's and 1
'
1558 x 2431 df
I need to plot an image in which every blank (NA
) field is filled with yellow and every 1
field is filled with green but every example I find has a much more simpler data frame or they don't have binary observations so I can't adapt their code to my problem.
I need to plot something like this. I extracted a portion of my data frame and took this screenshot after creating a few conditions in Microsoft Excel.
Thanks in advance.
Here's a start:
library(tidyverse)
# Fake data
set.seed(2)
dat = as.data.frame(replicate(30, sample(c(1,2,NA),50,replace=TRUE)))
dat$row = 1:nrow(dat)
# Convert data to long format
dat = gather(dat, col, value, -row) %>%
mutate(col = factor(col, levels=names(dat)))
ggplot(dat, aes(col, row, fill=factor(value))) +
geom_tile(colour="grey50") +
scale_fill_manual(values=c("1"="green", "2"="white"), na.value="yellow") +
scale_y_reverse(breaks=1:50, expand=c(0,0)) +
scale_x_discrete(position="top") +
labs(fill="Value") +
theme_classic()
Here's an alternative that uses image
. There are some caveats to this approach: it converts your NA
values to -1
for the purpose of plotting, and must use a matrix. So, your mileage may vary depending on what you need this for, but it's a quick way to visualize if that's all you're after.
mat <- matrix(sample(c(1, NA), 100, replace = TRUE), nrow = 10, ncol = 10,
dimnames = list(1:10, 1:10))
mat[is.na(mat)] <- -1
par(mar = c(9,5,5,5), xpd = TRUE)
xn <- as.numeric(colnames(mat))
yn <- as.numeric(rownames(mat))
image(xn, yn, mat, breaks = c(-1, 0, 1), col = c("yellow", "green"))
legend(3.5, -0.7, c("NA", "1"), fill = c("yellow", "green"), ncol = 2)