2012-07-18 119 views
2

我有一些我想要可視化的序列特徵信息。 下面是一些玩具數據(R-特異性碼以再生該數據是在端部)如何在ggplot2中的兩個關聯點之間添加多個線段?

  type index variable position 
... 
14   CDS 14 start 31129 
15  exon 15 start 32196 
16   CDS 16 start 32196 
17 stop_codon 17 start 32247 
18  exon  1  end 12166 
19   CDS  2  end 12166 
... 

我用於生成以下情節的命令是

qplot(position,type,data=m2data,color=type)+xlim(11950,15000) 

what I have so far

但我想添加「開始」和「結束」之間的線段,這些線段共享相同的「索引」 ,如下所示,這是我用油漆製作的。

What I want

如何我GGPLOT2 R中實現這一目標?

以下是數據

m2data<-structure(list(type = structure(c(2L, 1L, 3L, 2L, 1L, 2L, 1L, 
4L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 1L, 4L, 2L, 1L, 3L, 2L, 1L, 2L, 
1L, 4L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 1L, 4L), class = "factor", .Label = c("CDS", 
"exon", "start_codon", "stop_codon")), index = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
"11", "12", "13", "14", "15", "16", "17"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("start", "end"), class = "factor"), position= c(11955L, 
12026L, 12026L, 16677L, 16677L, 17745L, 17745L, 17787L, 18309L, 
28587L, 28658L, 28658L, 31129L, 31129L, 32196L, 32196L, 32247L, 
12166L, 12166L, 12028L, 16841L, 16841L, 17814L, 17786L, 17789L, 
18898L, 28798L, 28798L, 28660L, 31299L, 31299L, 32270L, 32246L, 
32249L)), .Names = c("type", "index", "variable", "position"), row.names = c(NA, 
-34L), class = "data.frame") 
+0

可能重複的[甘特式時間線圖(在鹼R)](http://stackoverflow.com/questions/9862519/gantt-style-time-line-plot-in -base-r) – 2012-07-18 23:13:32

+1

我實際上在基地提出了同樣的問題,但是Andrie給出了一個很好的ggplot2答案。我認爲你可能想從頭開始重塑你的數據集,從長到寬。 – 2012-07-18 23:14:32

回答

7

這是你的問題的解決方案。雖然你的問題與之前的問題很相似,但我認爲提供一個特定於你的數據集的答案仍然有用。

library(ggplot2) 
library(reshape2) 

# Use dcast (in reshape2 package) to create separate columns for start and end. 
dat = dcast(m2data, type + index ~ variable, value.var="position") 

plot_1 = ggplot(dat, aes(x=start, xend=end, y=type, yend=type, colour=type)) + 
     geom_segment(size=3) + 
     geom_point(size=3) + 
     geom_point(aes(x=end), size=3) 

ggsave(filename="plot_1.png", plot_1, width=10, height=2.5) 

enter image description here

+0

超級!正是我所期待的。 順便說一句,我想你可以在ggplot()而不是geom_segment()中傳遞aes中的xend和yend參數? – Alby 2012-07-19 16:10:38

+1

很高興幫助!我發現在最初的'ggplot()'調用中映射所有的美學是最方便的。但有時候這還不夠,就像我對geom_point()的兩次調用一樣。第一個隱式使用'aes(x = start)'。但爲了增加終點,需要第二次調用'aes()'。 – bdemarest 2012-07-19 17:04:43

+0

感謝這個非常好的答案和好看的圖!這也正是我需要的。 – WAF 2013-08-30 12:24:53

相關問題