Matherion - 4 months ago 17

R Question

In psychology, it's common to display histograms with an overlaying normal curve. Also showing the density of the observed values with geom_line would facilitate comparison to the normal curve, so I wrote another histogram function that does this (

`powerHist`

`userfriendlyscience`

`density`

But this is very slow, plus, I figured ggplot2 should be able to do this. One of the variables computed by

`stat_density`

`..scaled..`

`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');

This yields:

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

When replacing the

`scalingFactor`

`ggplot(mtcars, aes(x=mpg)) +`

geom_histogram(bins=20) +

geom_line(aes(y=..scaled.. * 10),

stat='density', color='red');

Also, when just using

`scalingFactor`

`ggplot(mtcars, aes(x=mpg)) +`

geom_histogram(bins=20) +

geom_line(aes(y=scalingFactor ),

stat='density', color='red');

So

`scalingFactor`

`..scaled..`

Has anybody run into this before? Is it known ggplot2 behavior that I just missed?

Answer

try with `aes_q(y=bquote(..scaled.. * .(scalingFactor)))`

(although I would think there is a bug somewhere, since the environment argument in ?ggplot suggests this shouldn't be needed, and in fact isn't needed when dealing with variables that don't come from a stat)