2

我有一些想要可视化的序列特征信息。这是一些玩具数据(重新生成数据的具体r代码在最后)

          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)

到目前为止我所拥有的

但是我想在“开始”和“结束”之间添加线段,它们共享相同的“索引”,就像我用油漆制作的一样。

我想要的是

如何在 R 中使用 ggplot2 实现这一点?

以下是数据

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")
4

1 回答 1

8

这是您的问题的解决方案。尽管您的问题与之前的问题非常相似,但我认为提供特定于您的数据集的答案仍然很有用。

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)

在此处输入图像描述

于 2012-07-19T00:22:39.323 回答