I've created a histogram/density plot function where I want the y axis to be count rather than density, but am having problems parameterizing its binwidth.
I am using examples based on http://docs.ggplot2.org/current/geom_histogram.html to illustrate my attempts.
Here's the successful plotMovies1 function. I followed the referenced url to make the y axis ..count.. instead of ..density.. Note that it uses a hardcoded .5 binwidth in two places, which is what I want to parameterize ...
# I want y axis as count, rather than density, and followed
# https://stat.ethz.ch/pipermail/r-help/2011-June/280588.html
plotMovies1 <- function() {
m <- ggplot(movies, aes(x = rating))
m <- m + geom_histogram(binwidth = .5)
m <- m + geom_density(aes(y = .5 * ..count..))
}

My first, failed naive attempt at parameterizing binwidth in a local bw in plotMovies2 ...
# Failed first attempt to parameterize binwidth
plotMovies2 <- function() {
bw <- .5
m <- ggplot(movies, aes(x = rating))
m <- m + geom_histogram(binwidth = bw)
# Error in eval(expr, envir, enclos) : object 'bw' not found
m <- m + geom_density(aes(y = bw * ..count..))
}
> print(plotMovies2())
Error in eval(expr, envir, enclos) : object 'bw' not found
I see discussion about passing the local environment to aes in ggplot at https://github.com/hadley/ggplot2/issues/743, but plotMovies3 also fails in the same fashion, failing to find the bw object ...
# Failed second attempt to parameterize binwidth, even after establishing
# aes environment, per https://github.com/hadley/ggplot2/issues/743
plotMovies3 <- function() {
bw <- .5
m <- ggplot(movies, aes(x = rating), environment = environment())
m <- m + geom_histogram(binwidth = bw)
# Error in eval(expr, envir, enclos) : object 'bw' not found
m <- m + geom_density(aes(y = bw * ..count..))
}
> print(plotMovies3())
Error in eval(expr, envir, enclos) : object 'bw' not found
I finally try setting a global, but it still fails to find the object ...
# Failed third attempt using global binwidth
global_bw <<- .5
plotMovies4 <- function() {
m <- ggplot(movies, aes(x = rating), environment = environment())
m <- m + geom_histogram(binwidth = global_bw)
# Error in eval(expr, envir, enclos) : object 'global_bw' not found
m <- m + geom_density(aes(y = global_bw * ..count..))
}
> print(plotMovies4())
Error in eval(expr, envir, enclos) : object 'global_bw' not found
Given plotMovies3 and plotMovies4, I am guessing it is not a straightforward environment issue. Can anyone shed any light on how I might resolve this? Again, my goal was to be able to create a histogram/density plot function where
- Its y axis is count rather than density, and
- Its binwidth could be parameterized (e.g., for manipulate)
global_bw <<- 0.5in no way creates a "global" variable. Using<-in this last example would have the same effect.<<-is simply a way of making a variable assignment in a different scope. If you had included that line inside your function you would have created an object in the global environment rather than the local one in your function.return(m)at the end, it might make things run more smoothly.bw= 0.5; m <- ggplot(movies, aes(x = rating)); m + geom_density(aes(y = bw * ..count..))