R:多点时间序列数据的散点图,ggplot ?,重塑?

时间:2013-11-08 22:20:56

标签: r ggplot2 scatter reshape2

我有以下格式的数据。列V1是感兴趣的基因组位置,列V4和V5是两个不同时间点的次要等位基因频率。我想制作一个简单的xy散点图,其中一条线连接从时间点1到时间点2(在y轴上绘制)的每个特定位置的等位基因频率。 (注意,我实际上有数百到数千个数据点。)

   V1    V2      V3          V4          V5
1 153 1/113   1/115 0.008849558 0.008695652
2 390 0/176 150/152 0.000000000 0.986842105
3 445 1/149   1/152 0.006711409 0.006578947
4 507 0/154 144/146 0.000000000 0.986301370
5 619 1/103  99/101 0.009708738 0.980198020
6 649 0/138 120/123 0.000000000 0.975609756

我觉得我应该能够用ggplot来完成这个,但我不知道如何去做,因为我不知道如何为每个基因组位置指定两个y值,也不指定一列作为一个类别。我怀疑数据需要以某种方式重新塑造。非常感谢任何帮助或建议!


更新

感谢所有给我建议的人。我不认为我非常清楚想要将时间点作为我的x轴而不是基因组位置 - 我的道歉。希望这张照片澄清了这一点!

我已使用以下代码成功生成了我想要制作的情节:

ggplot(dat)+ geom_segment(aes(x =“timepoint 1”,y = V4,xend =“timepoint2”,yend = V5))

这就是情节与更多数据点相似......

allelefreqtrajectories

我还没有改变轴标题并且还使用了边距,但这是一般的想法!

3 个答案:

答案 0 :(得分:1)

如果您的示例数据位于DF,那么

ggplot(DF) +
  geom_segment(aes(x=V4, y="timepoint 1", xend=V5, yend="timepoint 2"))

enter image description here

答案 1 :(得分:0)

with(dat, plot(x=V1, y=V5, ylim=c(0,1) ,type='n',
      xaxt="n", ylab="Allele Frequency", xlab="Genomic Location"))
with(dat, axis(1, V1,V1, cex.axis=0.7)   )
with( dat, arrows(x0=V1,x1=V1+10, y0=V4, y1=V5) )

您可以清理标签并调整颜色和箭头功能:

?arrows

enter image description here

答案 2 :(得分:0)

问题并不完全清楚,但我认为这就是你所追求的:

ggplot(d, aes(x=V1, y=V4, ymin=V4, ymax=V5)) 
  + geom_linerange() 
  + xlab('Genomic location') 
  + ylab('Minor allele frequency')

文档:http://docs.ggplot2.org/current/geom_linerange.html

enter image description here

相关问题