r - Parameterized ggplot2 histogram/density aes function cannot find object -
i've created histogram/density plot function want y axis count rather density, having problems parameterizing binwidth.
i using examples based on http://docs.ggplot2.org/current/geom_histogram.html illustrate attempts.
here's successful plotmovies1 function. followed referenced url make y axis ..count.. instead of ..density.. note uses hardcoded .5 binwidth in 2 places, want parameterize ...
# want y axis count, rather density, , 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 @ parameterizing binwidth in local bw in plotmovies2 ...
# failed first attempt 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 passing local environment aes in ggplot @ https://github.com/hadley/ggplot2/issues/743, plotmovies3 fails in same fashion, failing find bw object ...
# failed second attempt parameterize binwidth, 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 try setting global, still fails find 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 , plotmovies4, guessing not straightforward environment issue. can shed light on how might resolve this? again, goal able create histogram/density plot function where
- its y axis count rather density, and
- its binwidth parameterized (e.g., manipulate)
by no means beautiful if need workaround can use regular density
function
plotmovies5 <- function(binw=0.5) { m <- ggplot(movies, aes(x = rating)) m <- m + geom_histogram(binwidth = binw) wa <- density(x=movies$rating, bw = binw) wa <- as.data.frame(cbind(xvals = wa$x, yvals = wa$y * wa$n * binw)) m <- m + geom_point(data = wa, aes(x = xvals, y = yvals)) } print(plotmovies5(binw=0.25))
note still have tinkering variables density estimates not equal following show you:
binw = 0.5 m <- ggplot(movies, aes(x = rating)) m <- m + geom_density(aes(y = 0.5 * ..count..)) wa <- density(x=movies$rating, bw = binw) wa <- as.data.frame(cbind(xvals = wa$x, yvals = wa$y * wa$n * binw)) m <- m + geom_point(data = wa, aes(x = xvals, y = yvals)) m
Comments
Post a Comment