如何指定折线图的起点和终点

时间:2015-11-19 12:35:37

标签: r plot

我有一个有三列的矩阵。第一列表示染色体片段起始位点。第二列代表染色体片段末端位点。最后一列代表每个片段' log 2比率。

我想在R图中反映开始和结束值,并使我的情节类似于下图。

enter image description here

dat <- structure(list(V2 = c(10000L, 131335L, 176031L, 352723L, 530564L, 
568797L, 659000L, 732902L, 770670L, 825228L, 865581L, 874488L, 
879156L, 880557L, 886632L, 889175L, 891333L, 893991L, 894333L, 
896833L, 897469L, 898224L, 898959L, 899619L, 901876L, 905842L, 
907466L, 908851L, 911760L, 914600L, 915421L, 916640L, 934743L, 
948948L, 949722L, 967266L, 976796L, 978877L, 979677L, 981147L, 
981756L, 982280L, 982971L, 984604L, 985495L, 986917L, 987423L, 
993585L, 1015923L, 1019671L, 1022611L, 1026296L, 1060070L, 1078501L, 
1104425L, 1114964L, 1117599L, 1120290L, 1132927L, 1139342L, 1140646L, 
1147128L, 1148860L, 1152936L, 1154097L, 1158629L, 1163994L, 1168587L, 
1178339L, 1179751L, 1190685L, 1192397L, 1196601L, 1201279L, 1216003L, 
1217387L, 1221435L, 1222353L, 1223037L, 1226038L, 1227307L, 1230245L, 
1231304L, 1233228L, 1235465L, 1238226L, 1242363L, 1244719L, 1245194L, 
1246878L, 1247763L, 1248398L, 1249133L, 1250230L, 1254308L, 1256077L, 
1257363L, 1262230L, 1266735L, 1267789L), V3 = c(131334L, 175999L, 
352633L, 530522L, 568792L, 658998L, 732892L, 770612L, 825195L, 
865580L, 874488L, 879155L, 880555L, 886629L, 889175L, 891333L, 
893990L, 894332L, 896833L, 897468L, 898223L, 898958L, 899619L, 
901876L, 905841L, 907466L, 908851L, 911760L, 914599L, 915419L, 
916640L, 934742L, 948945L, 949722L, 967263L, 976795L, 978877L, 
979677L, 981146L, 981755L, 982279L, 982971L, 984603L, 985494L, 
986917L, 987423L, 993580L, 1015884L, 1019671L, 1022608L, 1026296L, 
1060025L, 1078501L, 1104424L, 1114964L, 1117594L, 1120289L, 1132922L, 
1139342L, 1140645L, 1147127L, 1148860L, 1152935L, 1154097L, 1158629L, 
1163993L, 1168586L, 1178336L, 1179751L, 1190685L, 1192397L, 1196600L, 
1201277L, 1216001L, 1217387L, 1221435L, 1222352L, 1223037L, 1226036L, 
1227307L, 1230244L, 1231304L, 1233228L, 1235465L, 1238225L, 1242363L, 
1244719L, 1245194L, 1246878L, 1247763L, 1248397L, 1249133L, 1250229L, 
1254308L, 1256077L, 1257363L, 1262229L, 1266734L, 1267789L, 1268314L
), V6 = c(0.8906, 0.564571, 0.517607, 0.828888, 2.340412, 0.947814, 
0.622437, 0.353472, 0.679258, 0.270922, 0.439973, -0.160465, 
-0.160465, 0.314315, 0.411712, 0.00946, 0.516281, -0.272939, 
-0.324303, 0.081196, -0.275235, -0.241181, 0.375588, 0.218047, 
-0.466568, 0.371192, -0.078003, -0.252443, -0.650992, 0.498923, 
0.188368, 0.066769, -0.266075, 0.492191, 0.208222, -0.141495, 
0.173296, 0.242322, 0.221307, 0.317365, 0.226184, -0.054156, 
0.028197, -0.345854, -0.046294, -0.382504, 0.48407, 0.088356, 
-0.404949, 0, 0.398811, 0.403124, 0.140951, 0.079401, 0.281893, 
0.130584, 0.043013, 0.64689, 0.121888, 0.291231, 0.199982, -0.445734, 
0.052196, 0.311258, 0.423083, 0.264615, 0.537361, 0.401688, -0.068018, 
-0.03458, -0.503766, 0.176659, 0.357922, 0.28657, 0.037475, 0.394485, 
0.028197, 0.288125, 0.754325, 0.229426, -0.131064, -0.120707, 
0.387246, 0.340037, 0.245528, 0.250323, 0.046693, -0.044335, 
0.420248, 0.492191, 0.306661, 0.113139, -0.0019, 0.09014, 0.195016, 
0.424498, 0.360882, 0.538669, 0.529492, 0.794279)), .Names = c("V2", 
"V3", "V6"), row.names = 102381:102480, class = "data.frame")

2 个答案:

答案 0 :(得分:3)

使用ggplot2,您可以执行以下操作:

mydat <- data.frame(V2 = c(10, 20, 15),
                    V3 = c(15, 30, 25),
                    V6 = c(10, 20, 30))
library(ggplot2)

ggplot(data = mydat) + geom_segment(aes(x = V2, xend = V3, y = V6, yend = V6))

这导致了这个情节: Plot

这看起来与你想要的相似吗?

使用给定数据编辑

使用相同的代码,但使用上面的数据集:

mydat <- structure(list(V2 = c(10000L, 131335L, 176031L.....

ggplot(data = mydat) + geom_segment(aes(x = V2, xend = V3, y = V6, yend = V6))

Plot2

答案 1 :(得分:1)

您只需使用segments,无需外部库。

就像在这个简单的例子中一样:

plot(NULL,xlim=c(-1,1),ylim=c(-1,1))
m <- matrix(rnorm(30),10,3)
segments(m[,1],m[,3],m[,2],m[,3])