如何按区域创建具有多个固定坐标的散点图?

时间:2017-05-01 21:56:22

标签: r plot ggplot2 data.table scatter-plot

我有data.table格式的数据,如下所示:

library(data.table)
dt

interval    location    type    y_axis 
01          40           "X"    20                 
01          136          "Y"    40
01          2803         "Z"    21
01          8973         "X"    32
....                     ....
02          3219         "Z"    32
02          4074         "Z"    93
02          8015         "Z"    15
....

有几个间隔,例如0102等。在每个时间间隔内,数据点位于1到10,000的范围内。在dt中,第一个数据点为40,下一个数据点为136,等等。

间隔02的范围也介于1到10,000之间。

我想创建一个散点图,使得每个间隔按比例绘制1到10000的范围。然后第一个点将绘制在40,下一个绘制在136,等等。我还想要一条垂直线,显示间隔的位置。

如果只有一个间隔,qplot

就会很简单
plotted = qplot(x=location, y= y_axis, colour = type, data=dt) 
     + geom_vline(xintercept = c(unique(dt$interval)), linetype="dashed",  color = "darkgray", size=1)  # taking the first unique 'interval' value to be the boundary line

虽然多个时间间隔不起作用。 x=location将所有location个数据点排序在一起。

怎么会这样做?

编辑:这是dput(head(dt, 50))

的结果
interval = c("01",                                                                                                                                                                                                              
 "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",                                                                                                                                                                                                          
 "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",                                                                                                                                                                                                          
 "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",                                                                                                                                                                                                          
 "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01",                                                                                                                                                                                                          
 "01", "01", "01", "01", "01"), location = c(1230, 1609,                                                                                                                                                                                                      
 1903, 2574, 3151, 3198, 3312, 3659, 3709,                                                                                                                                                                                                      
 3725, 4172, 4542, 4860, 4900, 5068, 5220,                                                                                                                                                                                                      
 5260, 5339, 5442, 5529, 5773, 6128, 6165,                                                                                                                                                                                                      
 6177, 6269, 6275, 6460, 7167, 7361, 7361,                                                                                                                                                                                                      
 8051, 8222, 8305, 8992, 9104, 9439, 9844,                                                                                                                                                                                                      
 10045, 10764, 10787, 11104, 11478, 11508,                                                                                                                                                                                                          
 11684, 12490, 12590, 12794, 12803, 13823,                                                                                                                                                                                                          
 13982), type = c("X", "X", "Y", "A", "A",                                                                                                                                                                                                              
     "B", "X", "X", "X", "B", "B", "A", "A", "A", "B", "B", "X",                                                                                                                                                                                                            
     "B", "Y", "X", "X", "Y", "Y", "C", "A", "X", "X", "Z", "Z",                                                                                                                                                                                                            
     "B", "X", "X", "A", "A", "Y", "X", "A", "X", "X", "Z", "Z",                                                                                                                                                                                                            
     "C", "X", "Y", "Y", "Z", "Z", "Z", "Z", "Z"),  y_axis = c(50, 55, 
    54, 58, 57, 46, 50, 55, 46, 42, 56, 55, 55, 45, 52, 51, 45, 48, 50,
     49, 53, 55, 45, 40, 49, 37, 52, 58, 52, 4, 58, 52, 49, 58, 50, 55, 
    56, 53, 58, 43, 55, 55, 44, 52, 59, 49, 53, 39, 60, 52)

0 个答案:

没有答案