首页 > 解决方案 > 用 ggplot2 镜像两个 PheWAS 图

问题描述

我有两个 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)))

我添加了一个示例图像,我想制作一个类似的图像,但我的颜色和我的标题在两个数字之间。

示例图片

标签: rggplot2plotfiguremirroring

解决方案


翻转第二个情节

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

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

修复 y 轴限制

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

修复 x 标签

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

组合地块

现在,您想要合并这些图。但是,第一个图在 x 轴上有很多信息,而第二个图没有。这意味着它们具有不同的高度。我喜欢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 轴上为每一行分配一个位置。


推荐阅读