Luiz Gustavo
Luiz Gustavo

Reputation: 37

Two types of variables in a single Heatmap (using R)

I have to plot data from immunized animals in a way to visualize possible correlations in protection. As a background, when we vaccinate an animal it produces antibodies, which might or not be linked to protection. We immunized bovine with 9 different proteins and measured antibody titers which goes up to 1.5 (Optical Density (O.D.)). We also measured tick load that goes up to 5000. Each animal have different titers for each protein and different tick loads, maybe some proteins are more important for protection than the others, and we think that a heatmap could illustrate it.

TL;DR: Plot a heatmap with one variable (Ticks) that goes from 6 up to 5000, and another variable (Prot1 to Prot9) that goes up to 1.5.

A sample of my data:

Animal  Group   Ticks   Prot1   Prot2   Prot3   Prot4   Prot5   Prot6   Prot7   Prot8   Prot9
G1-54-102   control 3030    0.734   0.402   0.620   0.455   0.674   0.550   0.654   0.508   0.618
G1-130-102  control 5469    0.765   0.440   0.647   0.354   0.528   0.525   0.542   0.481   0.658
G1-133-102  control 2070    0.367   0.326   0.386   0.219   0.301   0.231   0.339   0.247   0.291
G3-153-102  vaccinated  150 0.890   0.524   0.928   0.403   0.919   0.593   0.901   0.379   0.647
G3-200-102  vaccinated  97  1.370   0.957   1.183   0.658   1.103   0.981   1.051   0.534   1.144
G3-807-102  vaccinated  606 0.975   0.706   1.058   0.626   1.135   0.967   0.938   0.428   1.035

I have little knowledge in R, but I'm really excited to learn more about it. So feel free to put whatever code you want and I will try my best to understand it.

Thank you in advance. Luiz

Upvotes: 0

Views: 2350

Answers (2)

Jannik Buhr
Jannik Buhr

Reputation: 1937

In the newest version of ggplot2 / the tidyverse, you don't even need to explicitly load the viridis-package. The scale is included via scale_fill_viridis_c(). Exciting times!

Upvotes: 0

www
www

Reputation: 39154

Here is an option to use the ggplot2 package to create a heatmap. You will need to convert your data frame from wide format to long format. It is also important to convert the Ticks column from numeric to factor if the numbers are discrete.

library(tidyverse)
library(viridis)

dat2 <- dat %>%
  gather(Prot, Value, starts_with("Prot"))

ggplot(dat2, aes(x = factor(Ticks), y = Prot, fill = Value)) +
  geom_tile() +
  scale_fill_viridis()

enter image description here

DATA

dat <- read.table(text = "Animal  Group   Ticks   Prot1   Prot2   Prot3   Prot4   Prot5   Prot6   Prot7   Prot8   Prot9
'G1-54-102'   control 3030    0.734   0.402   0.620   0.455   0.674   0.550   0.654   0.508   0.618
'G1-130-102'  control 5469    0.765   0.440   0.647   0.354   0.528   0.525   0.542   0.481   0.658
'G1-133-102'  control 2070    0.367   0.326   0.386   0.219   0.301   0.231   0.339   0.247   0.291
'G3-153-102'  vaccinated  150 0.890   0.524   0.928   0.403   0.919   0.593   0.901   0.379   0.647
'G3-200-102'  vaccinated  97  1.370   0.957   1.183   0.658   1.103   0.981   1.051   0.534   1.144
'G3-807-102'  vaccinated  606 0.975   0.706   1.058   0.626   1.135   0.967   0.938   0.428   1.035",
                  header = TRUE, stringsAsFactors = FALSE)

Upvotes: 1

Related Questions