为什么 stat_density2d 函数的 bins 参数未知? (地图)

Why is bins parameter unknown for the stat_density2d function? (ggmap)

我已经坚持了好几个小时了。当我 运行 这个 :

library(ggmap)
set.seed(1)
n=100

df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))

TestData <- ggplot (data = df) +
  stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)),bins=4, geom = "polygon",) +
  geom_point(aes(x = x, y = y)) +
  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))

我收到此错误消息:

Error: Unknown parameters: bins

有人知道为什么吗?

更新:

经过与 Roland 的深入讨论(查看评论),他确定这可能是一个回归错误并提交了错误报告。


由于问题是"why is the bins parameter unknown?",我花了相当多的时间研究它,所以我会回答它。

您的示例显然来自 2013 年 10 月的 link,其中使用了参数。 How to correctly interpret ggplot's stat_density2d

然而,它从来都不是一个文档化的参数,也不清楚它是否在那里被使用。它可能是传递给 stat_density2d 使用的其他库(如 MASS)的参数。

我们可以通过摆脱 scale_fill_manual 调用并使用这个来使代码工作:

library(ggmap)
set.seed(1)
n=100

df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))

TestData <- ggplot (data = df) +
  stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)), geom = "polygon",) +
  geom_point(aes(x = x, y = y)) 
#  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))
print(TestData)

这产生了这个:

由于这看起来与 2013 年 10 月 link 中发布的原始情节大不相同,我想说 stat_density2d 从那时起已被广泛重写,或者 MASS:kde2d(或另一个 MASS 例程),并且不再接受 bin 参数。也不清楚该参数是否做过任何事情(阅读 link)。

然而,您可以改变参数 nh - 从 stat_density2d 的角度来看也没有记录(据我所知)。

好的,添加这个作为第二个答案,因为我认为第一个答案中的描述和评论很有用,我不想合并它们。基本上我认为必须有一种简单的方法来恢复回归的功能。一段时间后,学习了一些关于 ggplot2 的基础知识,我通过重写一些 ggplot2 函数来实现它:

library(ggmap)
library(ggplot2)

# -------------------------------
# start copy from stat-density-2d.R

stat_density_2d <- function(mapping = NULL, data = NULL, geom = "density_2d",
                            position = "identity", contour = TRUE,
                            n = 100, h = NULL, na.rm = FALSE,bins=0,
                            show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = StatDensity2d,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      contour = contour,
      n = n,
      bins=bins,
      ...
    )
  )
}

stat_density2d <- stat_density_2d

StatDensity2d <- 
  ggproto("StatDensity2d", Stat,
          default_aes = aes(colour = "#3366FF", size = 0.5),

          required_aes = c("x", "y"),

          compute_group = function(data, scales, na.rm = FALSE, h = NULL,
                                   contour = TRUE, n = 100,bins=0) {
            if (is.null(h)) {
              h <- c(MASS::bandwidth.nrd(data$x), MASS::bandwidth.nrd(data$y))
            }

            dens <- MASS::kde2d(
              data$x, data$y, h = h, n = n,
              lims = c(scales$x$dimension(), scales$y$dimension())
            )
            df <- data.frame(expand.grid(x = dens$x, y = dens$y), z = as.vector(dens$z))
            df$group <- data$group[1]

            if (contour) {
              #  StatContour$compute_panel(df, scales,bins=bins,...) # bad dots...
              if (bins>0){
                StatContour$compute_panel(df, scales,bins)
              } else {
                StatContour$compute_panel(df, scales)
              }
            } else {
              names(df) <- c("x", "y", "density", "group")
              df$level <- 1
              df$piece <- 1
              df
            }
          }
  )

# end copy from stat-density-2d.R
# -------------------------------

set.seed(1)
n=100

df <- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))

TestData <- ggplot (data = df) +
  stat_density2d(aes(x = x, y = y,fill = as.factor(..level..)),bins=5,geom = "polygon") +
  geom_point(aes(x = x, y = y)) +
  scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))
print(TestData)

产生结果。请注意,改变 bins 参数现在具有预期的效果,无法通过改变 n 参数来复制。