Haakonkas
Haakonkas

Reputation: 1041

Fitting annotations to y-positions in ggplot heatmap

I have the following data:

structure(list(id = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("1", "2", 
"3", "4", "5", "6"), class = "factor"), species = structure(c(2L, 
2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 
4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 
4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 
2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 1L, 
4L), .Label = c("Broiler", "Pig", "Red Fox", "Wild bird"), class = "factor"), 
    MIC = structure(c(1L, 2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 
    4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L, 1L, 
    2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 
    4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L, 1L, 
    2L, 3L, 3L, 4L, 5L, 1L, 2L, 3L, 3L, 4L, 5L), .Label = c("0.12", 
    "0.25", "1", "2", "4"), class = "factor"), gene = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 
    6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
    8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
    10L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("gyrA", "parC", 
    "marR", "marA", "qnrS", "qnrA", "qnrB", "qnrD", "ydhE", "mcbE", 
    "oqxAB"), class = "factor"), value = c(0, 0, 1, 1, 1, 0, 
    1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 
    1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 
    1, 1, 1), label = c("0", "0", "1 - Red Fox", "1 - Wild bird", 
    "1 - Broiler", "0", "1 - Pig", "1 - Pig", "0", "1 - Wild bird", 
    "0", "1 - Wild bird", "0", "1 - Pig", "1 - Red Fox", "1 - Wild bird", 
    "0", "0", "0", "1 - Pig", "1 - Red Fox", "1 - Wild bird", 
    "1 - Broiler", "1 - Wild bird", "1 - Pig", "0", "1 - Red Fox", 
    "1 - Wild bird", "0", "1 - Wild bird", "0", "0", "1 - Red Fox", 
    "1 - Wild bird", "0", "1 - Wild bird", "0", "0", "0", "0", 
    "1 - Broiler", "0", "1 - Pig", "0", "1 - Red Fox", "0", "0", 
    "0", "1 - Pig", "0", "1 - Red Fox", "0", "1 - Broiler", "0", 
    "0", "1 - Pig", "1 - Red Fox", "0", "0", "1 - Wild bird", 
    "0", "0", "0", "1 - Wild bird", "1 - Broiler", "1 - Wild bird"
    )), class = "data.frame", .Names = c("id", "species", "MIC", 
"gene", "value", "label"), row.names = c(NA, -66L))

This data is plotted as follows:

library(ggplot2)
library(forcats)
colors <- c("#b13da1", "#00b551" , "#fff723" , "#ff0022")

ggplot(plot_data, aes(gene,fct_reorder(id, as.numeric(species)), fill = label))+
geom_tile(color = "black")+
  theme_classic()+
  scale_fill_manual(values = c("white", colors), "Value")+
  scale_x_discrete(position = "top")+
  theme(axis.title = element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 0.5),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_blank())+
  coord_fixed(ratio = 0.3)

Which looks like this: enter image description here

I would like to create an additional "column" with data from the column named "MIC" for each row in the figure, corresponding to the respective sample, something like this:

enter image description here

I have tried adding geom_text(inherit.aes = FALSE, aes(MIC,fct_reorder(id, as.numeric(species))), label = plot_data$MIC, size = 2) to the ggplot code but that only produces extra columns at the beginning with the MIC values as column names as well, so the yare scattered between the columns and not neatly aligned into one. Is there an easy way to do this? (The figure was previously created in this question)

Advice on visualizing this in any other way is also appreciated.

Upvotes: 0

Views: 1372

Answers (1)

Axeman
Axeman

Reputation: 35307

You need to set you x aesthetic correctly, and then manually put the MIC column at the end in your x scale. Finally, we need to only use a subset of the data, since the MIC just gets repeated over and over again. A single gene will do:

ggplot(plot_data, aes(gene,fct_reorder(id, as.numeric(species)), fill = label))+
  geom_tile(color = "black")+
  geom_text(aes('MIC', fct_reorder(id, as.numeric(species)), label = MIC), 
            subset(plot_data, gene == "gyrA"), inherit.aes = FALSE, size = 3) +
  theme_classic()+
  scale_fill_manual(values = c("white", colors), "Value")+
  scale_x_discrete(position = "top", limits = c(levels(plot_data$gene), 'MIC'))+
  theme(axis.title = element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 0.5),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_blank())+
  coord_fixed(ratio = 0.3)

enter image description here

Upvotes: 1

Related Questions