繁体   English   中英

只绘制一个点的子集?

[英]Plotting only a subset of the points?

我正在尝试使用ggplot绘制包含大约2900万个值的大型数据集的CDF曲线。 我计算它的方式是这样的:

mycounts = ddply(idata.frame(newdata), .(Type), transform, ecd = ecdf(Value)(Value))
plot = ggplot(mycounts, aes(x=Value, y=ecd))

这需要很长时间才能绘制。 我想知道是否有一种干净的方法只绘制该数据集的样本(例如,每10点或50点)而不影响实际结果?

我不确定您的数据结构,但简单的sample调用可能就足够了:

n <- nrow(mycounts)                              # number of cases in data frame
mycounts <- mycounts[sample(n, round(n/10)), ]   # get an n/10 sample to the same data frame

在绘制数据之前,您可以将数据集量化为足够的分辨率,而不是每个n点吗? 这样,你就不必绘制你不需要(或看不到)的分辨率。

这是你可以做到的一种方式。 (我在下面写的函数是通用的,但该示例使用了您问题中的名称。)

library(ggplot2)
library(plyr)

## A data set containing two ramps up to 100, one by 1, one by 10

tens <- data.frame(Type = factor(c(rep(10, 10), rep(1, 100))),
                   Value = c(1:10 * 10, 1:100))


## Given a data frame and ddply-style arguments, partition the frame
## using ddply and summarize the values in each partition with a
## quantized ecdf.  The resulting data frame for each partition has
## two columns: value and value_ecdf.

dd_ecdf <- function(df, ..., .quantizer = identity, .value = value) {
  value_colname <- deparse(substitute(.value))
  ddply(df, ..., .fun = function(rdf) {
    xs <- rdf[[value_colname]]
    qxs <- sort(unique(.quantizer(xs)))
    data.frame(value = qxs, value_ecdf = ecdf(xs)(qxs))
  })
}


## Plot each type's ECDF (w/o quantization)

tens_cdf <- dd_ecdf(tens, .(Type), .value = Value)
qplot(value, value_ecdf, color = Type, geom = "step", data = tens_cdf)



## Plot each type's ECDF (quantizing to nearest 25)

rounder <- function(...) function(x) round_any(x, ...)
tens_cdfq <- dd_ecdf(tens, .(Type), .value = Value, .quantizer = rounder(25))
qplot(value, value_ecdf, color = Type, geom = "step", data = tens_cdfq)

虽然原始数据集和ecdf集有110行,但量化的ecdf集大大减少了:

> dim(tens)
[1] 110   2
> dim(tens_cdf)
[1] 110   3
> dim(tens_cdfq)
[1] 10  3
> tens_cdfq
   Type value value_ecdf
1     1     0       0.00
2     1    25       0.25
3     1    50       0.50
4     1    75       0.75
5     1   100       1.00
6    10     0       0.00
7    10    25       0.20
8    10    50       0.50
9    10    75       0.70
10   10   100       1.00

我希望这有帮助! :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM