geom_raster使用其他颜色代码可视化缺失值 [英] geom_raster to visualize missing values with additional colorcode

查看:110
本文介绍了geom_raster使用其他颜色代码可视化缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是我之前的问题的后续内容:我想使用R中ggplot2中的geom_raster可视化数据框中的缺失信息",同时还使用颜色编码突出显示一些其他数据结构.

I would like to visualize the "missing info" in a data frame using geom_raster from ggplot2 in R while also highlighting some additional data structure using color-coding.

解决方案尝试:

library(tidyverse)
x11()
airquality %>%
  mutate(id = row_number()) %>%
  gather(-c(id,Month), key = "key", value = "val") %>%
  mutate(isna = is.na(val)) %>%
  mutate(Month=as.factor(ifelse(isna==TRUE,NA,Month)))  %>%
  ggplot(aes(key, id, fill = Month)) +
    geom_raster() +
    labs(x = "Variable",
           y = "Row Number", title = "Missing values in rows") +
    coord_flip()

这几乎是我想要的,但是最好将月份和NA传说分开.那可能吗? (请注意,我的系统不允许我使用透明度(alpha).)

This is almost what I want, but it would be nicer to separate the month and NA legends. Is that possible? (Note that my system does not allow me to use transparency (alpha)).

推荐答案

在这里,我删除了NA的图例.如果这不能正确满足您的目的,我可以考虑采用一种骇人听闻的解决方案,为数据添加另一个图例,以防丢失.

Here, I removed the legend for NA. If this doesn't serve your purpose properly, I can think of a hacky solution to add another legend for data vs. missing.

library(tidyverse)

airquality %>%
  mutate(id = row_number()) %>%
  gather(-c(id,Month), key = "key", value = "val") %>%
  mutate(isna = is.na(val)) %>%
  mutate(Month_Dummy=as.factor(ifelse(isna==TRUE,NA,Month)))  %>%
  mutate(Month=as.factor(Month))  %>% 
  ggplot() +
  geom_raster(aes(key, id, fill = Month)) +
  geom_raster(aes(key, id, fill = Month_Dummy)) +
  labs(x = "Variable",
       y = "Row Number", title = "Missing values in rows") +
  coord_flip()

我能想到的hacky解决方案是为其中一个缺失项添加一个geom_point,并将其用于缺失数据点的图例.就外观而言,它不是最好的,但我能想到的唯一解决方案.

The hacky solution that I can think of is adding a geom_point for just one of the missing and used that for the legend of missing data points. It's not the best in terms of appearance, but is the only solution I can think of.

library(tidyverse)

airquality %>%
  mutate(id = row_number()) %>%
  gather(-c(id,Month), key = "key", value = "val") %>%
  mutate(isna = is.na(val)) %>%
  mutate(Month_Dummy=as.factor(ifelse(isna==TRUE,NA,Month)))  %>%
  mutate(Month=as.factor(Month))  -> aqdf

ggplot(data = aqdf, aes(key, id)) +
  geom_raster(aes(fill = Month)) +
  geom_raster(aes(fill = Month_Dummy)) +
  geom_point(data=aqdf[aqdf$isna==TRUE,][1,], 
             aes(NA, id, colour = "NA"),
             inherit.aes = FALSE) +
  scale_color_manual(values=c("grey50")) +
  labs(x = "Variable", y = "Row Number", 
       title = "Missing values in rows", color = "Missing") +
  coord_flip() +
  theme(legend.key = element_rect(fill = "grey50")) 

这篇关于geom_raster使用其他颜色代码可视化缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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