简体   繁体   English

显示NA值+更改颜色范围| R中的ggplot2

[英]Show NA values + change the color range | ggplot2 in R

I have an input file file1.txt : 我有一个输入文件file1.txt

rs113565588 SIFT    0.306
rs113565588 Polyphen2   0
rs113565588 MutationAssessor    0.345
rs61729896  SIFT    NA
rs61729896  Polyphen2   NA
rs61729896  MutationAssessor    NA
rs61755283  SIFT    NA
rs61755283  Polyphen2   NA
rs61755283  MutationAssessor    NA
rs777439738 SIFT    NA
rs777439738 Polyphen2   NA
rs777439738 MutationAssessor    NA

And this is my R code to plot this table as a heatmap: 这是我的R代码,用于将此表绘制为热图:

library(ggplot2)
mydata <- read.table("file1.txt", header=FALSE, 
                     sep="\t")

ggplot(data = mydata, aes(x=V1, y=V2, fill=V3)) + 
  geom_tile() + 
  geom_text(aes(V1, V2, label = V3), color = "black", size = 4)

And this the plot I got: 这是我得到的情节:

在此处输入图片说明

I need to: 我需要:

  1. Label NA values as well, rather than leave them as a gray blank; 也应标记NA值,而不是将其保留为灰色空白;
  2. Change the color range rather than this default one. 更改颜色范围而不是此默认值。

Any help or suggestions? 有什么帮助或建议吗?

Something like the following can get you started: 类似以下内容可以帮助您入门:

library(dplyr)

ggplot(data = mydata, aes(x = V1, y = V2)) + 
  geom_tile(aes(fill = V3)) + 
  geom_text(data = . %>% mutate(V3 = ifelse(is.na(V3), "NA", as.character(V3))),
            aes(label = V3), 
            color = "black", size = 4) +
  scale_fill_gradient(low = "gold", high = "firebrick4", na.value = "grey")

情节

Explanations: 说明:

  1. x = V1, y = V2 are common aesthetic mappings shared across both geom layers. x = V1, y = V2是在两个几何层之间共享的常见美学贴图。 Leave them in ggplot() to avoid typing for each layer. 将其保留在ggplot()以避免为每个图层键入内容。
  2. fill = V3 is only needed for geom_tile() . fill = V3仅对于geom_tile()才需要。 Place it there rather than the main one, as we do not need geom_text() to inherit it as well. 将它放在那里而不是主要的地方,因为我们也不需要geom_text()来继承它。
  3. We want NA values to be interpreted as "NA" for labels only , so we modify the dataset passed to geom_text() . 我们希望仅将NA值解释为标签的 “ NA”,因此我们修改了传递给geom_text()的数据集。 data = . refers to the data inherited from the main ggplot() . 引用从主ggplot()继承的数据。 . %>% mutate(...) . %>% mutate(...) uses the pipe operator & mutate function from dplyr package to convert NA values to "NA". . %>% mutate(...)使用dplyr包中的管道运算符和mutate函数将NA值转换为“ NA”。 If you prefer some other label, you can use that as well. 如果您喜欢其他标签,也可以使用它。
  4. scale_fill_gradient() allows you to set different colours for the two ends of the scale, as well as change the fill colour for NA values. scale_fill_gradient()允许您为刻度的两端设置不同的颜色,以及更改NA值的填充颜色。 Here is a handy lookup table for colour names recognised in R . 这是一个方便的查找表,用于识别R中的颜色名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM