简体   繁体   English

用数据框中的另一列标记x轴

[英]Labeling x-axis with another column from dataframe

I have a dataframe derived from the output of running GWAS. 我有一个从运行的GWAS的输出派生的数据框。 Each row is a SNP in the genome, with its Chromosome, Position, and P.value. 每行都是基因组中的SNP,具有染色体,位置和P.value。 From this dataframe, I'd like to generate a Manhattan Plot where the x-axis goes from the first SNP on Chr 1 to the last SNP on Chr 5 and the y-axis is the -log10(P.value). 从这个数据框中,我想生成一个曼哈顿图,其中x轴从Chr 1上的第一个SNP到Chr 5上的最后一个SNP,而y轴是-log10(P.value)。 To do this, I generated an Index column to plot the SNPs in the correct order along the x-axis, however, I would like the x-axis to be labeled by the Chromosome column instead of the Index. 为此,我生成了一个Index列以沿x轴以正确顺序绘制SNP,但是,我希望x轴由染色体列而不是Index标记。 Unfortunately, I cannot use Chromosome to plot my x-axis because then all the SNPs on any given Chromosome would be plotted in a single column of points. 不幸的是,我不能使用染色体来绘制我的x轴,因为任何给定染色体上的所有SNP都将被绘制在一个点列中。

Here is an example dataframe to work with: 这是一个可以使用的示例数据框:

library(tidyverse)

df <- tibble(Index = seq(1, 500, by = 1),
             Chromosome = rep(seq(1, 5, by = 1), each = 100),
             Position = rep(seq(1, 500, by = 5), 5),
             P.value = sample(seq(1e-5, 1e-2, by = 1e-5), 500, replace = TRUE))

And the plot that I have so far: 到目前为止,我的情节是:

df %>%
    ggplot(aes(x = Index, y = -log10(P.value), color = as.factor(Chromosome))) +
    geom_point()

I have tried playing around with the scale_x_discrete option, but haven't been able to figure out a solution. 我尝试过使用scale_x_discrete选项,但是还没有找到解决方案。

Here is an example of a Manhattan Plot I found online. 这是我在网上找到的曼哈顿情节的示例。 See how the x-axis is labeled according to the Chromosome? 看看如何根据染色体标记x轴? That is my desired output. 那是我想要的输出。

曼哈顿情节示例

geom_jitter is your friend: geom_jitter是您的朋友:

df %>%
    ggplot(aes(x = Chromosome, y = -log10(P.value), color = as.factor(Chromosome))) +
    geom_jitter()

Edit given OP's comment: 编辑给定OP的评论:

Using base R plot, you could do: 使用基数R图,您可以执行以下操作:

cols = sample(colors(), length(unique(df$Chromosome)))[df$Chromosome]

plot(df$Index, -log10(df$P.value), col=cols, xaxt="n")
axis(1, at=c(50, 150, 250, 350, 450), labels=c(1:5))

You'll need to specify exactly where you want each chromosome label to be for the axis function. 您需要确切指定每个染色体标签在axis功能中的位置。 Thanks to this post . 感谢这篇文章

Edit #2: 编辑#2:

I found an answer using ggplot2 . 我使用ggplot2找到了答案。 You can use the annotate function to plot your points by coordinates, and the scale_x_discrete function (as you suggested) to place the labels in the x axis according to chromosome. 您可以使用annotate功能按坐标绘制点,并使用scale_x_discrete函数(如您建议的那样)根据染色体将标签放置在x轴上。 We also need to define the pos vector to get the position of labels for the plot. 我们还需要定义pos向量以获取图的标签位置。 I used the mean value of the Index column for each group as an example, but you can define it by hand if you wish. 我以每个组的“ Index列的平均值为例,但是您可以根据需要手动定义它。

pos <- df %>% 
    group_by(Chromosome) %>% 
    summarize(avg = round(mean(Index))) %>% 
    pull(avg)

ggplot(df) +
    annotate("point", x=df$Index, y=-log10(df$P.value),
          color=as.factor(df$Chromosome)) +
    scale_x_discrete(limits = pos, 
          labels = unique(df$Chromosome))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM