gpt4 book ai didi

r - 参数化的 ggplot2 直方图/密度 aes 函数找不到对象

转载 作者:行者123 更新时间:2023-12-04 11:27:24 27 4
gpt4 key购买 nike

我创建了一个直方图/密度图函数,我希望 y 轴是计数而不是密度,但在参数化其 binwidth 时遇到问题。

我正在使用基于 http://docs.ggplot2.org/current/geom_histogram.html 的示例来说明我的尝试。

这是成功的 plotMovies1 函数。我遵循了引用的 url使 y 轴 ..count.. 而不是 ..density.. 请注意,它在两个地方使用了硬编码的 .5 binwidth,这就是我想要的参数化...

# 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..))
}

histogram/density with count as y axis and hardcoded binwidth

我第一次尝试在 plotMovies2 中的本地 bw 中参数化 binwidth,但失败了 ...

# 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

我在 https://github.com/hadley/ggplot2/issues/743 看到关于将本地环境传递给 ggplot 中的 aes 的讨论, 但 plotMovies3 也以同样的方式失败,找不到 bw 对象...

# 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

我终于尝试设置一个全局的,但它仍然未能找到对象......

# 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

鉴于 plotMovies3 和 plotMovies4,我猜这不是一个简单的环境问题。谁能阐明我如何解决这个问题?同样,我的目标是能够创建直方图/密度图函数,其中

  1. 它的 y 轴是计数而不是密度,并且
  2. 它的 binwidth 可以参数化(例如,用于操作)

最佳答案

绝不漂亮,但如果您需要解决方法,您可以使用常规的 density 函数

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))

请注意,您仍然需要对变量进行一些修改,因为密度估计并不完全相等,如下所示:

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

关于r - 参数化的 ggplot2 直方图/密度 aes 函数找不到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com