将网格线添加到与轴断点无关的ggplot

时间:2012-09-11 12:52:19

标签: r ggplot2 gridlines

我有两个不同类别的核苷酸序列(A,T,C和G的有序字符序列)丰度的数据。我想使用ggplot将覆盖范围显示为序列上的线图。我已经走得太远了,还有几个障碍要跳。样本数据:

> dput(so.sample)
structure(list(`1_mes_wt` = c(0, 0, 4.25897346349789, 10.0666645500859, 
10.0666645500859, 48.3974257215669, 78.2102399660521, 250.698665237717, 
250.698665237717, 268.702507606139, 271.79994285232, 301.225577691032, 
301.225577691032, 301.225577691032, 301.225577691032, 311.292242241118, 
311.292242241118, 311.292242241118, 311.292242241118, 311.292242241118, 
311.292242241118, 311.292242241118, 311.292242241118, 311.292242241118, 
311.292242241118, 311.292242241118, 311.292242241118, 321.746086196977, 
321.746086196977, 321.746086196977, 319.616599465228, 308.969165806483, 
285.931991163017, 204.817905653671, 195.332010212244, 170.165348837029, 
170.165348837029, 170.165348837029, 170.165348837029, 37.3628126570497, 
40.8474273090025, 22.456405534807, 22.456405534807, 17.8102526655366, 
17.8102526655366, 17.8102526655366, 17.8102526655366, 17.8102526655366, 
17.8102526655366, 17.8102526655366, 17.8102526655366, 15.6807659337877, 
15.6807659337877, 15.6807659337877, 15.6807659337877, 15.6807659337877, 
15.6807659337877, 15.6807659337877, 15.6807659337877, 15.6807659337877, 
15.6807659337877, 15.6807659337877, 15.6807659337877, 15.6807659337877, 
15.6807659337877, 15.6807659337877, 5.22692197792922, 5.22692197792922, 
5.22692197792922, 5.22692197792922, 5.22692197792922, 5.22692197792922, 
5.22692197792922, 5.22692197792922, 5.22692197792922, 5.22692197792922, 
5.22692197792922, 1.74230732597641, 1.93589702886268, 1.93589702886268, 
4.25897346349789, 2.71025584040775, 2.71025584040775, 68.5307548217387, 
89.6320324363419, 90.0192118421144, 93.310236791181, 93.1166470882947, 
93.5038264940673, 93.8910058998398, 93.8910058998398, 93.8910058998398, 
93.8910058998398, 93.8910058998398, 93.8910058998398, 93.8910058998398, 
93.8910058998398, 93.8910058998398, 93.8910058998398, 93.8910058998398, 
93.8910058998398, 93.8910058998398, 93.8910058998398, 93.8910058998398, 
93.8910058998398, 93.6974161969535, 93.6974161969535, 93.6974161969535, 
93.6974161969535, 93.6974161969535, 93.6974161969535, 93.6974161969535, 
93.6974161969535, 93.6974161969535, 89.2448530305694, 77.2422914516208, 
1.16153821731761, 0, 0, 0, 0), `2_mes_wt` = c(0, 0, 13.3249362857652, 
41.0267775114349, 41.3774337294814, 100.988990797378, 100.988990797378, 
341.188500159198, 343.643093685523, 359.422623497613, 381.163309016493, 
450.242583971645, 450.242583971645, 450.242583971645, 450.242583971645, 
450.242583971645, 450.242583971645, 450.242583971645, 450.242583971645, 
450.242583971645, 450.242583971645, 450.242583971645, 450.242583971645, 
450.242583971645, 450.242583971645, 450.242583971645, 450.242583971645, 
450.242583971645, 450.242583971645, 450.242583971645, 450.242583971645, 
426.047304926439, 366.085091640496, 308.928128098924, 201.977981594756, 
201.977981594756, 205.133887557174, 205.133887557174, 205.133887557174, 
85.5601172033343, 98.8850534890995, 17.1821546842762, 17.1821546842762, 
17.1821546842762, 17.1821546842762, 17.1821546842762, 17.1821546842762, 
16.8314984662297, 16.8314984662297, 16.8314984662297, 16.8314984662297, 
50.8451516167356, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 52.5984327069678, 52.5984327069678, 
52.5984327069678, 52.5984327069678, 39.2734964212026, 37.5202153309704, 
42.0787461655743, 54.7023700152465, 54.7023700152465, 54.7023700152465, 
113.261958429004, 114.313927083143, 114.313927083143, 116.067208173376, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 82.0535550228698, 
82.0535550228698, 82.0535550228698, 82.0535550228698, 74.3391182258479, 
63.4687754664078, 0, 0, 0, 0, 0), nucl = 47738064:47738184, base = c("T", 
"C", "A", "A", "A", "A", "A", "A", "G", "A", "C", "T", "A", "G", 
"T", "C", "A", "A", "G", "T", "G", "C", "A", "G", "T", "A", "G", 
"T", "G", "A", "G", "A", "A", "G", "G", "G", "G", "G", "G", "A", 
"A", "A", "G", "T", "G", "T", "A", "G", "A", "A", "C", "A", "G", 
"G", "A", "G", "T", "T", "C", "A", "A", "T", "C", "T", "G", "T", 
"A", "A", "C", "T", "G", "A", "C", "T", "G", "T", "G", "A", "A", 
"C", "A", "A", "T", "C", "A", "A", "T", "T", "G", "A", "G", "A", 
"T", "A", "A", "C", "T", "C", "A", "C", "T", "A", "C", "C", "T", 
"T", "C", "G", "G", "A", "C", "C", "A", "G", "C", "C", "A", "A", 
"T", "G", "C")), .Names = c("1_mes_wt", "2_mes_wt", "nucl", "base"
), row.names = c(NA, -121L), class = "data.frame")

要显示序列(基础)和序列位置(nucl),我将位置绘制为因子,然后使用文本geom将序列标记为轴。它运作得相当好:

pdf("so_sample.pdf",width=15,height=7)
ggplot(melt(so.sample,id.vars=c("nucl","base")), aes(factor(nucl), 
       value, group=variable, colour=variable)) + 
  geom_line() + geom_text(y=-5,size=3,aes(label=base))
dev.off()

enter image description here

然而,将这些大数字作为因素进行绘制会导致重叠,我无论如何都不需要看到每个位置标签。所以我删除了一些休息时间。

pdf("so_sample2.pdf",width=15,height=7)
ggplot(melt(so.sample,id.vars=c("nucl","base")), aes(factor(nucl), 
       value, group=variable, colour=variable)) + 
  geom_line() + geom_text(y=-5,size=3,aes(label=base)) + 
  scale_x_discrete(breaks=seq(min(so.sample$nucl),
                   max(so.sample$nucl),10))
dev.off()

enter image description here

我几乎就在那里,但在删除休息时,我还删除了在字符序列旁边特别有用的次要网格线。是否有任何方法可以添加与scale_x_discrete中指定的中断无关的小网格线?

还有一个问题:序列标记的工作原理是因为它为每个分组在完全相同的位置绘制相同的字符,这些标签的最终颜色由绘制组的顺序决定。我希望序列是中性色,但我不知道是否有可能将geom_text这些值与分组无关。

1 个答案:

答案 0 :(得分:1)

保留nucl,而不是将其包含在factor的通话中,您可以使用与scale_x_continuous不同的scale_x_discrete minor_breaks参数。< / p>