仅从包含所有值的数据框绘制上限量

时间:2015-03-31 07:13:08

标签: r scatter-plot boxplot quantile

我有一个大型数据帧df,其中包含非唯一标识符(Cell.ID)列表和该标识符中的信息。它看起来像这样:

    Cell.ID Volume
1   025001G 2.08
2   025001G 0.30
3   025001G 0.99
4   025001G 0.60
5   025001G 0.43
6   025001G 0.24
7   025001G 0.59
8   025001R 1.74
9   025001R 1.09
10  025001R 0.58
11  025001R 0.75
12  025001R 0.62
13  025002G 8.59
14  025002G 1.26
15  025002R 6.31
16  025002R 0.56
17  025003G 1.95
18  025003G 2.18
19  025003G 0.21

我想做的是绘制Y轴对应于Volume的图,X坐标对应于特定Cell.ID的实例数。这部分是直截了当的,但我希望每个对象的Y坐标要么是跨越上两个分位数的框,要么是代表第二高分位数的点。使用tapply(df$Volume,quantile)table(df$Cell.ID)我能够创建一个类似下面的数据框,其中包含制作所述图的必要信息。 Freq包含有关特定Cell.ID(行名)显示的次数的信息,而Quantile包含有关该Cell.ID中对象的卷分布的信息。

 row.names       quantile                        Var1     Freq
1   010001G c(0.27, 0.27, 0.325, 0.6125, 1.31)    010001G   4
2   010001R c(0.22, 0.365, 0.51, 0.655, 0.8)     010001R    2
3   010002G c(0.67, 0.8025, 0.935, 1.0675, 1.2)  010002G    2
4   010002R c(0.25, 0.41, 0.57, 0.73, 0.89)      010002R    2
5   010003G c(0.22, 0.295, 0.345, 0.3725, 0.38)  010003G    4
6   010003R c(0.22, 0.2675, 0.315, 0.3625, 0.41) 010003R    2
7   010004G c(0.35, 0.41, 0.625, 1.165, 2.2)     010004G    4
8   010004R c(0.2, 0.4075, 0.615, 0.8225, 1.03)  010004R    2
9   010005G c(3.95, 3.95, 3.95, 3.95, 3.95)      010005G    1
10  010005R c(0.47, 0.775, 1.08, 2.53, 3.98)     010005R    3
11  010006G c(0.25, 0.98, 1.71, 2.98, 4.25)      010006G    3

然而,我仍然坚持如何仅从分位数列中选择每行中的某些分位数来绘制。我尝试了一些事情,但却出现了这样的错误:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y 

1 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,你不需要所有的分位数,只需要其中的一个或两个。所以你可以试试这样的东西:

Q75 <- tapply(df$Volume, df$Cell.ID, quantile, probs = 0.75)
freq <- table(df$Cell.ID)
plot(x = as.vector(freq), y = Q75, 
     xlab = "Frequency", ylab = "75th Quantile")

或者对于第75和第95分位数:

Q7595 <- do.call(rbind.data.frame, 
                 tapply(df$Volume, df$Cell.ID, quantile, 
                        probs = c(0.75, 0.95), simplify = TRUE))
## Empty plot
matplot(x = as.vector(freq), y = Q7595, type = "n", 
        xlab = "Frequency", ylab = "75th and 95th Quantiles")
## Boxes 
rect(xleft = as.vector(freq) - 0.25, xright = as.vector(freq) + 0.25, 
     ytop = Q7595[,1], ybottom = Q7595[,2])

结果如下: enter image description here

当然它需要一些美学上的改变,但我希望它有所帮助, 亚历

相关问题