我有5〜非常大的载体(〜108个MM条目),所以没有任何剧情/东西我R中与他们无关花费相当长的时间。
我试图想象他们的分布(直方图),并想知道什么是叠加R中的柱状图分布而不会太长的最佳途径。 我想先适应分布直方图,然后绘制所有配电线路结合在一起的一个阴谋。
你对怎么做一些建议吗?
让我们说我的向量:
x1, x2, x3, x4, x5.
我想使用此代码: 叠加直方图与R中GGPLOT2
我使用3个矢量(R未能做到的情节)的代码的示例:
n = length(x1)
dat <- data.frame(xx = c(x1, x2, x3),yy = rep(letters[1:3],each = n))
ggplot(dat,aes(x=xx)) +
geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) +
geom_histogram(data=subset(dat,yy == 'b'),fill = "blue", alpha = 0.2) +
geom_histogram(data=subset(dat,yy == 'c'),fill = "green", alpha = 0.2)
但需要很久才能推出的情节,并最终踢我出去R.对如何有效地使用GGPLOT2为大载体任何想法? 在我看来,我不得不创建一个数据帧,5个* 108毫米条目,然后情节,在我的情况非常低效的。
谢谢!
下面是RCPP的一个小片段,垃圾箱数据非常有效 - 我的电脑上大约需要一秒钟来斌亿点意见:
library(Rcpp)
cppFunction('
std::vector<int> bin3(NumericVector x, double width, double origin = 0) {
int bin, nmissing = 0;
std::vector<int> out;
NumericVector::iterator x_it = x.begin(), x_end;
for(; x_it != x.end(); ++x_it) {
double val = *x_it;
if (ISNAN(val)) {
++nmissing;
} else {
bin = (val - origin) / width;
if (bin < 0) continue;
// Make sure there\'s enough space
if (bin >= out.size()) {
out.resize(bin + 1);
}
++out[bin];
}
}
// Put missing values in the last position
out.push_back(nmissing);
return out;
}
')
x8 <- runif(1e8)
system.time(bin3(x8, 1/100))
# user system elapsed
# 1.373 0.000 1.373
这就是说, hist
是蛮快的在这里太:
system.time(hist(x8, breaks = 100, plot = F))
# user system elapsed
# 7.281 1.362 8.669
它的简单使用bin3
作直方图或频率多边形:
# First we create some sample data, and bin each column
library(reshape2)
library(ggplot2)
df <- as.data.frame(replicate(5, runif(1e6)))
bins <- vapply(df, bin3, 1/100, FUN.VALUE = integer(100 + 1))
# Next we match up the bins with the breaks
binsdf <- data.frame(
breaks = c(seq(0, 1, length = 100), NA),
bins)
# Then melt and plot
binsm <- subset(melt(binsdf, id = "breaks"), !is.na(breaks))
qplot(breaks, value, data = binsm, geom = "line", colour = variable)
仅供参考,我有原因bin3
手是我的工作,就如何使这个速度在GGPLOT2默认:)