首页 文章

如何使用相关数据框内部或外部的变量在ggplot中转换美学'on the fly'?

提问于
浏览
1

在心理学中,显示具有重叠正常曲线的直方图是常见的 . 同时用geom_line显示观察值的密度将有助于与正常曲线进行比较,因此我编写了另一个直方图函数来执行此操作( userfriendlyscience 包中的 powerHist ) . 但是,对于大型向量(目前使用1670万个数据点),它的执行速度非常慢,所以我试图让它更快 . 我曾经使用 density 来手动计算密度估计值,然后将它们与bin中的最大数据点数相乘以对其进行缩放以匹配直方图 .

但这很慢,而且,我认为ggplot2应该可以做到这一点 . 由 stat_density 计算的变量之一是 ..scaled.. ,这是密度估计值,最大值为1.现在我只需将其乘以 . 但ggplot2赢了't find the variable I use. Multiplying it with a constant works fine, but whether I place the variable in the dataframe I pass on to ggplot2 or not doesn'似乎很重要:ggplot2无法找到它 .

scalingFactor <- max(table(cut(mtcars$mpg, breaks=20)));
dat <- data.frame(mpg = mtcars$mpg,
                  scalingFactor = scalingFactor);
ggplot(mtcars, aes(x=mpg)) +
  geom_histogram(bins=20) +
  geom_line(aes(y=..scaled.. * scalingFactor),
            stat='density', color='red');

这会产生:

Error in eval(expr, envir, enclos) : object 'scalingFactor' not found

使用常规数字替换 scalingFactor 时,它可以:

ggplot(mtcars, aes(x=mpg)) +
  geom_histogram(bins=20) +
  geom_line(aes(y=..scaled.. * 10),
            stat='density', color='red');

Histogram with hardcoded scaled densitycurve

此外,当它自己只使用 scalingFactor 时,它也有效:

ggplot(mtcars, aes(x=mpg)) +
  geom_histogram(bins=20) +
  geom_line(aes(y=scalingFactor ),
            stat='density', color='red');

Histogram with horizontal line showing scalingFactor

所以 scalingFactor 似乎可用;乘法是可用的;显然 ..scaled.. 可用 . 但是,将它们组合似乎也失败了 . 我在这里想念的是什么?我可以使用stat'或其他东西生成的变量来计算't find anything on ' . . .

有没有人遇到这个?是否知道ggplot2的行为,我错过了?

1 回答

  • 3

    试试 aes_q(y=bquote(..scaled.. * .(scalingFactor)))

    (虽然我认为某处存在一个错误,因为?ggplot中的环境参数表明不需要这样做,实际上在处理不是来自stat的变量时不需要)

相关问题