- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 ~ 5 个非常大的向量(~ 108 MM 条目),所以我在 R 中用它们做的任何绘图/东西都需要相当长的时间。
我正在尝试可视化它们的分布(直方图),并且想知道在 R 中叠加它们的直方图分布而不花费太长时间的最佳方法是什么。我想首先将分布拟合到直方图,然后将所有分布线拟合到一个图中。
您对如何做到这一点有什么建议吗?
假设我的向量是:
x1, x2, x3, x4, x5.
我正在尝试使用此代码:Overlaying histograms with ggplot2 in R
我用于 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*108MM 条目的数据框,然后进行绘图,在我的情况下效率非常低。
谢谢!
最佳答案
这里有一个 Rcpp 的小片段,它非常有效地对数据进行分类 - 在我的计算机上,大约需要一秒钟来分类 100,000,000 个观察结果:
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 中的默认速度:)
关于r - SuperImpose 直方图适合一张图 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661065/
下面的脚本说明了我的问题: library(reshape2) set.seed(1) dummy.df <- data.frame(var_a=sample(letters[1:5],200,rep
在最终成功找到一种在iPhone上将多个语音文件连接成一个音频文件的方法之后,我现在正尝试在音频文件的长度上叠加一个音频文件。 所以基本上我有两个.m4a文件: voice.m4a,例如大约10秒。
我有 ~ 5 个非常大的向量(~ 108 MM 条目),所以我在 R 中用它们做的任何绘图/东西都需要相当长的时间。 我正在尝试可视化它们的分布(直方图),并且想知道在 R 中叠加它们的直方图分布而不
我有一个图形,它由 imshow() 显示的图像、轮廓和 quiver() 设置的矢量场组成。我根据另一个标量对矢量场进行了着色。在我图的右边,我制作了一个colorbar()。此 colorbar(
我有一个包含 12 页内容的 PDF。我有另一个 PDF(只有 1 页),它是图形模板。 PDF(内容和模板)均为A4 尺寸,水平方向。 对于 PDF 内容的每一页,我需要应用相同的模板作为背景 这是
如何以编程方式将按钮标题叠加在按钮图像顶部,并将它们放置在彼此重叠的按钮框架的正中心? let button = UIButton() button.setImage(coolpic, for .no
我正在尝试在 Superimposing content from one PDF into another PDF 中设置创建日期和修改日期示例 sandbox.stamper.SuperImpos
我想在彩色图像上叠加一个二进制掩码,这样在掩码“打开”的地方,像素值的变化量我可以设置。结果应如下所示: 我正在使用 OpenCV 2.4 和 Python 2.7.6。我有一种方法效果很好,但速度很
我是一名优秀的程序员,十分优秀!