R中二进制数据帧的条件着色 [英] Conditional coloring of a binary data frame in R

查看:132
本文介绍了R中二进制数据帧的条件着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个巨大的数据框(1558个变量的obs)填充了 NA 的和 1 '




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()

这篇关于R中二进制数据帧的条件着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆