LamaMo
LamaMo

Reputation: 626

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

I have an input file 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:

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:

enter image description here

I need to:

  1. Label NA values as well, rather than leave them as a gray blank;
  2. Change the color range rather than this default one.

Any help or suggestions?

Upvotes: 0

Views: 1205

Answers (1)

Z.Lin
Z.Lin

Reputation: 29075

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

plot

Explanations:

  1. x = V1, y = V2 are common aesthetic mappings shared across both geom layers. Leave them in ggplot() to avoid typing for each layer.
  2. fill = V3 is only needed for geom_tile(). Place it there rather than the main one, as we do not need geom_text() to inherit it as well.
  3. We want NA values to be interpreted as "NA" for labels only, so we modify the dataset passed to geom_text(). data = . refers to the data inherited from the main ggplot(). . %>% mutate(...) uses the pipe operator & mutate function from dplyr package to convert NA values to "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. Here is a handy lookup table for colour names recognised in R.

Upvotes: 2

Related Questions