使用 ggplot2 镜像两个 PheWAS 图

Mirror two PheWAS plots with ggplot2

我有两个 PheWAS 图,两者的类别数(x 轴,20 个类别)相同。我想将它们放在同一个图上,在 y 轴上镜像其中一个,但将 x 轴标题留在中间。

示例数据:

dataset1 <- data.frame(
  chr = c(1, 3, 5, 6, 7, 2, 8, 3, 6, 4, 6, 3),
  pos = c(445578, 659990, 789689, 678599, 97890, 67899, 9867647, 675890, 799030, 8609000, 789900, 90907878),
  p = c(0.05, 0.34, 0.55, 0.05, 0.67, 0.01, 0.34, 0.55, 0.76, 0.88, 0.12, 0.22),
  description = factor(c("Metabolic", "Metabolic", "Immunological", "BodyStructures", "Cardiovascular", "Hematological", "Nutritional", "Environment", "Psychiatric", "Cognitive", "Musculoskeletal", "Opthalmological"))
)

dataset2 <- data.frame(
  chr = c(1, 3, 5, 6, 7, 2, 8, 3, 6, 4, 6, 3),
  pos = c(444358, 647389, 76379, 64789, 1456378, 5647839, 452678, 65789, 657839, 3562789, 15367384, 2647489),
  p = c(5e-05, 0.4554, 0.003, 0.6789, 0.6783, 0.00568, 0.789, 0.7799, 0.00457, 0.7899, 0.35678, 0.3678),
  description = factor(c("Metabolic", "Metabolic", "Immunological", "BodyStructures", "Cardiovascular", "Hematological", "Nutritional", "Environment", "Psychiatric", "Cognitive", "Musculoskeletal", "Opthalmological"))
)

为了创建我的两个地块,我使用以下脚本:

地块 1:

library(ggplot2)
library(RColorBrewer)

colourCount <- length(unique(dataset1$description))
getPalette <- colorRampPalette(brewer.pal(9, "Set1"))

PheWAS_1 <- ggplot(dataset1, aes(x=description, y=-log(p), colour = description)) + 
  geom_jitter(mapping=aes(x=as.factor(description), y=-log10(p))) +
  theme_classic() + 
  scale_colour_manual(values = getPalette(colourCount)) +
  theme(axis.text.x=element_text(angle = 75, vjust = 0.5, hjust=1, size = 10, margin=margin(-30,0,0,0))) + 
  labs(color="description", size="Effect size", x="Phenotype Group", y="-log10(p-value)") +
  geom_hline(yintercept=-log10(7.45E-07), color="gray32", linetype="dashed", size=1, alpha=0.5)+
  theme(legend.position = "none") +
  theme(axis.title.x = element_text(margin = margin(40, 0, 0, 0)))

情节 2:

colourCount <- length(unique(dataset2$description))
getPalette <- colorRampPalette(brewer.pal(9, "Set1"))

PheWAS_2 <- ggplot(dataset2, aes(x=description, y=-log(p), colour = description)) + 
  geom_jitter(mapping=aes(x=as.factor(description), y=-log10(p))) +
  theme_classic() + 
  scale_colour_manual(values = getPalette(colourCount)) +
  theme(axis.text.x=element_text(angle = 75, vjust = 0.5, hjust=1, size = 10, margin=margin(-30,0,0,0))) + 
  labs(color="description", size="Effect size", x="Phenotype Group", y="-log10(p-value)") +
  geom_hline(yintercept=-log10(2.83E-06), color="gray32", linetype="dashed", size=1, alpha=0.5)+
  theme(legend.position = "none") +
  theme(axis.title.x = element_text(margin = margin(40, 0, 0, 0)))

我添加了一张示例图片,我想制作一张类似的图片,但我的颜色和标题介于两张图片之间。

翻转第二个情节

为此,我们需要添加两个函数:

  • scale_y_reverse:这将翻转y轴; 0 在顶部,10 在底部。
  • scale_x_discrete(position = top):这会将 x-axis 放在顶部。

修复 y-axis 限制

最好对两个地块保持相同的 y-axis 限制,以使它们具有可比性。因此,我们必须向第一个图提供 ylim()。对于第二个图,我们已经有了 scale_y_reverse,所以我们可以在那里提供我们的限制。

修复 x 标签

由于您只希望标签出现一次,因此您必须在第二个图中对 theme(axis.text.x)theme(axis.title.x) 使用 element_blank()。同样,我会删除第一个图中的 x-axis 标题以保持平衡。

合并地块

现在,您想合并这些图。然而,第一个图有很多关于 x-axis 的信息,而第二个图没有。这意味着它们具有不同的高度。我喜欢使用 cowplot::plot_grid 来组合绘图,因为它允许您设置绘图的相对高度。在这种情况下,我们可以用它来解释两个地块之间的高度差异。

最终代码

PheWAS_1 <- ggplot(dataset1, aes(x = description, y = -log(p), colour = description)) + 
  geom_jitter() +
  theme_classic() + 
  scale_colour_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0, size = 10),
        axis.title.x = element_blank(),
        legend.position = "none") + 
  labs(color = "description", size = "Effect size", x = "", y = "-log10(p-value)") +
  ylim(c(0, 10)) +
  geom_hline(yintercept = -log10(7.45E-07), color = "gray32", linetype = "dashed", size = 1, alpha = 0.5)


PheWAS_2 <- ggplot(dataset2, aes(x = description, y = -log(p), colour = description)) + 
  geom_jitter() +
  theme_classic() + 
  scale_colour_manual(values = getPalette(colourCount)) +
  theme(axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        legend.position = "none") + 
  labs(color = "description", size = "Effect size", x = "Phenotype Group", y = "-log10(p-value)") +
  scale_y_reverse(limits = c(10, 0)) +
  scale_x_discrete(position = "top") +
  geom_hline(yintercept = -log10(2.83E-06), color = "gray32", linetype = "dashed", size = 1, alpha = 0.5)

cowplot::plot_grid(PheWAS_1, PheWAS_2, ncol = 1, rel_heights = c(1, 0.75))

最后的笔记

设置代码的方式是引入抖动。这不是一个好方法,因为理想情况下,两个图之间的峰会匹配。您最好在 x-axis.

上为每一行分配一个位置