【发布时间】:2021-01-23 05:42:40
【问题描述】:
我正在尝试为我的样本中确定的微生物群落创建一个属级别的热图。
问题
我想在我的热图中添加总和和行总数(每个样本的所有基因的总丰度和所有样本的每个基因的总丰度)。但是,我很难使用代码,以便在不更改数据和以前的代码的情况下添加总值。
我尝试的解决方法:
我尝试使用以下代码绘制热图。然而,由此产生的情节很奇怪。
我的数据框:
Gene Sample1 Sample2 Sample3 Sample4 Sample5 Total
A 0.0186 1.578 3.478 0.0045 0.569 5.648
B 0.0009 0.125 1.254 5.890 1.590 8.8599
C 2.567 0.897 0.0026 1.285 2.648 7.3996
D 10.421 0.743 0.0152 0.479 6.489 18.1472
... ... ... ... ... ... ...
Total 34.49 11.1 11.72 18.19 24.52 100
我的转换数据:
Gene Sample Abundance
A 1 0.0186
B 1 0.0009
C 1 2.567
D 1 10.421
... ... ...
我的代码:
p1 <- ggplot(data=heatmap,
mapping=aes(x= Sample, y= Gene, fill= Abundance)) +
geom_tile() +
facet_grid (~ Sample, scales = "free", space = "free") +
scale_fill_distiller(name = "Relative Abundance (%)", palette = "RdYlGn") +
theme(text = element_text(size=12),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 11, colour = "black"),
strip.text.y = element_text(angle = 0),
legend.direction = "horizontal",
legend.position = "bottom")
+ scale_x_discrete(position = "top")
p2 <- p1 + geom_point(data = heatmap, aes(color = Abundance),size = 10, shape = 19)
+ scale_color_gradient2(low = "red", mid = "white",high = "grey", midpoint = 0)
+ geom_text(data = heatmap, size = 3, aes(label = round(Abundance,2)))
p2
结果:
感谢有关如何更改绘图以更好地可视化的任何建议以及有关如何添加水平和垂直总和的任何建议。
【问题讨论】:
-
嗯。在我的机器上,您的代码运行良好。我得到了一个带有标签的漂亮热图以及带有总计的行和列。
-
@stefan 你得到了我得到的类似情节吗?
-
没有。使用
tidy::pivot_longer将数据重新整形为 long 并运行代码后,我得到了这个图 i.imgur.com/O72GAxp.png -
@stefan 看起来真不错。您介意提供代码以便我更好地理解吗?
-
@stefan 但我实际上想将总和分开,这样它们就不会影响比例。喜欢这个:i.stack.imgur.com/P5Xbd.jpg。但我不知道如何操作代码。