将注释拟合到ggplot热图中的y位置

时间:2018-03-27 13:31:10

标签: r ggplot2

我有以下数据:

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

该数据绘制如下:

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)

看起来像这样: enter image description here

我想创建一个额外的“列”,其中包含图中每行的名为“MIC”的列中的数据,对应于相应的示例,如下所示:

enter image description here

我已经尝试将geom_text(inherit.aes = FALSE, aes(MIC,fct_reorder(id, as.numeric(species))), label = plot_data$MIC, size = 2)添加到ggplot代码中,但是在开头只生成额外的列,MIC值也作为列名,因此yare在列之间分散,而不是整齐地排列成一列。 是否有捷径可寻? (该图以前是在this问题中创建的)

也欢迎以任何其他方式对此进行可视化的建议。

1 个答案:

答案 0 :(得分:1)

您需要正确设置x美学,然后手动将MIC列放在x比例的末尾。最后,我们只需要使用数据的子集,因为MIC只是一遍又一遍地重复。一个基因就可以了:

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